contextmetadata.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package store
  2. import "encoding/json"
  3. // DockerContext represents the docker context metadata
  4. type DockerContext struct {
  5. Name string `json:",omitempty"`
  6. Metadata ContextMetadata `json:",omitempty"`
  7. Endpoints map[string]interface{} `json:",omitempty"`
  8. }
  9. // Type the context type
  10. func (m *DockerContext) Type() string {
  11. if m.Metadata.Type == "" {
  12. return defaultContextType
  13. }
  14. return m.Metadata.Type
  15. }
  16. // ContextMetadata is represtentation of the data we put in a context
  17. // metadata
  18. type ContextMetadata struct {
  19. Type string
  20. Description string
  21. StackOrchestrator string
  22. AdditionalFields map[string]interface{}
  23. }
  24. // AciContext is the context for the ACI backend
  25. type AciContext struct {
  26. SubscriptionID string `json:",omitempty"`
  27. Location string `json:",omitempty"`
  28. ResourceGroup string `json:",omitempty"`
  29. }
  30. // MobyContext is the context for the moby backend
  31. type MobyContext struct{}
  32. // ExampleContext is the context for the example backend
  33. type ExampleContext struct{}
  34. // MarshalJSON implements custom JSON marshalling
  35. func (dc ContextMetadata) MarshalJSON() ([]byte, error) {
  36. s := map[string]interface{}{}
  37. if dc.Description != "" {
  38. s["Description"] = dc.Description
  39. }
  40. if dc.StackOrchestrator != "" {
  41. s["StackOrchestrator"] = dc.StackOrchestrator
  42. }
  43. if dc.Type != "" {
  44. s["Type"] = dc.Type
  45. }
  46. if dc.AdditionalFields != nil {
  47. for k, v := range dc.AdditionalFields {
  48. s[k] = v
  49. }
  50. }
  51. return json.Marshal(s)
  52. }
  53. // UnmarshalJSON implements custom JSON marshalling
  54. func (dc *ContextMetadata) UnmarshalJSON(payload []byte) error {
  55. var data map[string]interface{}
  56. if err := json.Unmarshal(payload, &data); err != nil {
  57. return err
  58. }
  59. for k, v := range data {
  60. switch k {
  61. case "Description":
  62. dc.Description = v.(string)
  63. case "StackOrchestrator":
  64. dc.StackOrchestrator = v.(string)
  65. case "Type":
  66. dc.Type = v.(string)
  67. default:
  68. if dc.AdditionalFields == nil {
  69. dc.AdditionalFields = make(map[string]interface{})
  70. }
  71. dc.AdditionalFields[k] = v
  72. }
  73. }
  74. return nil
  75. }