Skip to content

Commit

Permalink
#48 - Extract snapshot IDs from the server links & include them in th…
Browse files Browse the repository at this point in the history
…e output
  • Loading branch information
ldmberman committed Mar 30, 2016
1 parent 596a7f8 commit 7c1f077
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions models/server/entities.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package server

import (
"encoding/json"
"strings"

"github.com/centurylinkcloud/clc-go-cli/models"
"github.com/centurylinkcloud/clc-go-cli/models/alert"
"github.com/centurylinkcloud/clc-go-cli/models/customfields"
Expand Down Expand Up @@ -56,5 +59,41 @@ type IPAddresses struct {

type Snapshot struct {
Name string
Id string
Links []models.LinkEntity
}

func (s *Snapshot) UnmarshalJSON(data []byte) error {
// Here we are getting the snapshot ID out of the server links. Looks
// rather hacky but there doesn't seem to be another way for retrieving it.
id := func() string {
m := struct {
Links []struct {
Rel string
Href string
}
}{}
if err := json.Unmarshal(data, &m); err != nil {
return ""
}
for _, link := range m.Links {
if link.Rel != "self" {
continue
}
tokens := strings.Split(link.Href, "/")
return tokens[len(tokens)-1]
}
return ""
}()

if id != "" {
s.Id = id
}

type SimpleSnapshot Snapshot
return json.Unmarshal(data, &struct {
*SimpleSnapshot
}{
SimpleSnapshot: (*SimpleSnapshot)(s),
})
}

0 comments on commit 7c1f077

Please sign in to comment.