contextmetadata.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import "encoding/json"
  15. // DockerContext represents the docker context metadata
  16. type DockerContext struct {
  17. Name string `json:",omitempty"`
  18. Metadata ContextMetadata `json:",omitempty"`
  19. Endpoints map[string]interface{} `json:",omitempty"`
  20. }
  21. // Type the context type
  22. func (m *DockerContext) Type() string {
  23. if m.Metadata.Type == "" {
  24. return DefaultContextType
  25. }
  26. return m.Metadata.Type
  27. }
  28. // ContextMetadata is represtentation of the data we put in a context
  29. // metadata
  30. type ContextMetadata struct {
  31. Type string
  32. Description string
  33. StackOrchestrator string
  34. AdditionalFields map[string]interface{}
  35. }
  36. // AciContext is the context for the ACI backend
  37. type AciContext struct {
  38. SubscriptionID string `json:",omitempty"`
  39. Location string `json:",omitempty"`
  40. ResourceGroup string `json:",omitempty"`
  41. }
  42. // EcsContext is the context for the AWS backend
  43. type EcsContext struct {
  44. CredentialsFromEnv bool `json:",omitempty"`
  45. Profile string `json:",omitempty"`
  46. }
  47. // AwsContext is the context for the ecs plugin
  48. type AwsContext EcsContext
  49. // LocalContext is the context for the local backend
  50. type LocalContext struct{}
  51. // ExampleContext is the context for the example backend
  52. type ExampleContext struct{}
  53. // MarshalJSON implements custom JSON marshalling
  54. func (dc ContextMetadata) MarshalJSON() ([]byte, error) {
  55. s := map[string]interface{}{}
  56. if dc.Description != "" {
  57. s["Description"] = dc.Description
  58. }
  59. if dc.StackOrchestrator != "" {
  60. s["StackOrchestrator"] = dc.StackOrchestrator
  61. }
  62. if dc.Type != "" {
  63. s["Type"] = dc.Type
  64. }
  65. if dc.AdditionalFields != nil {
  66. for k, v := range dc.AdditionalFields {
  67. s[k] = v
  68. }
  69. }
  70. return json.Marshal(s)
  71. }
  72. // UnmarshalJSON implements custom JSON marshalling
  73. func (dc *ContextMetadata) UnmarshalJSON(payload []byte) error {
  74. var data map[string]interface{}
  75. if err := json.Unmarshal(payload, &data); err != nil {
  76. return err
  77. }
  78. for k, v := range data {
  79. switch k {
  80. case "Description":
  81. dc.Description = v.(string)
  82. case "StackOrchestrator":
  83. dc.StackOrchestrator = v.(string)
  84. case "Type":
  85. dc.Type = v.(string)
  86. default:
  87. if dc.AdditionalFields == nil {
  88. dc.AdditionalFields = make(map[string]interface{})
  89. }
  90. dc.AdditionalFields[k] = v
  91. }
  92. }
  93. return nil
  94. }