This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 252
Store context type in metadata to make retrocompatibility with previous contexts easier (potentially switching back and forth) #200
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package store | ||
|
||
import "encoding/json" | ||
|
||
// DockerContext represents the docker context metadata | ||
type DockerContext struct { | ||
Name string `json:",omitempty"` | ||
Metadata ContextMetadata `json:",omitempty"` | ||
Endpoints map[string]interface{} `json:",omitempty"` | ||
} | ||
|
||
// Type the context type | ||
func (m *DockerContext) Type() string { | ||
if m.Metadata.Type == "" { | ||
return defaultContextType | ||
} | ||
return m.Metadata.Type | ||
} | ||
|
||
// ContextMetadata is represtentation of the data we put in a context | ||
// metadata | ||
type ContextMetadata struct { | ||
Type string | ||
Description string | ||
StackOrchestrator string | ||
AdditionalFields map[string]interface{} | ||
} | ||
|
||
// AciContext is the context for the ACI backend | ||
type AciContext struct { | ||
SubscriptionID string `json:",omitempty"` | ||
Location string `json:",omitempty"` | ||
ResourceGroup string `json:",omitempty"` | ||
} | ||
|
||
// MobyContext is the context for the moby backend | ||
type MobyContext struct{} | ||
|
||
// ExampleContext is the context for the example backend | ||
type ExampleContext struct{} | ||
|
||
// MarshalJSON implements custom JSON marshalling | ||
func (dc ContextMetadata) MarshalJSON() ([]byte, error) { | ||
s := map[string]interface{}{} | ||
if dc.Description != "" { | ||
s["Description"] = dc.Description | ||
} | ||
if dc.StackOrchestrator != "" { | ||
s["StackOrchestrator"] = dc.StackOrchestrator | ||
} | ||
if dc.Type != "" { | ||
s["Type"] = dc.Type | ||
} | ||
if dc.AdditionalFields != nil { | ||
for k, v := range dc.AdditionalFields { | ||
s[k] = v | ||
} | ||
} | ||
return json.Marshal(s) | ||
} | ||
|
||
// UnmarshalJSON implements custom JSON marshalling | ||
func (dc *ContextMetadata) UnmarshalJSON(payload []byte) error { | ||
var data map[string]interface{} | ||
if err := json.Unmarshal(payload, &data); err != nil { | ||
return err | ||
} | ||
for k, v := range data { | ||
switch k { | ||
case "Description": | ||
dc.Description = v.(string) | ||
case "StackOrchestrator": | ||
dc.StackOrchestrator = v.(string) | ||
case "Type": | ||
dc.Type = v.(string) | ||
default: | ||
if dc.AdditionalFields == nil { | ||
dc.AdditionalFields = make(map[string]interface{}) | ||
} | ||
dc.AdditionalFields[k] = v | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package store | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ContextTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *ContextTestSuite) TestDockerContextMetadataKeepAdditionalFields() { | ||
c := ContextMetadata{ | ||
Description: "test", | ||
Type: "aci", | ||
StackOrchestrator: "swarm", | ||
AdditionalFields: map[string]interface{}{ | ||
"foo": "bar", | ||
}, | ||
} | ||
jsonBytes, err := json.Marshal(c) | ||
Expect(err).To(BeNil()) | ||
Expect(string(jsonBytes)).To(Equal(`{"Description":"test","StackOrchestrator":"swarm","Type":"aci","foo":"bar"}`)) | ||
|
||
var c2 ContextMetadata | ||
err = json.Unmarshal(jsonBytes, &c2) | ||
Expect(err).To(BeNil()) | ||
Expect(c2.AdditionalFields["foo"]).To(Equal("bar")) | ||
Expect(c2.Type).To(Equal("aci")) | ||
Expect(c2.StackOrchestrator).To(Equal("swarm")) | ||
Expect(c2.Description).To(Equal("test")) | ||
} | ||
|
||
func TestPs(t *testing.T) { | ||
RegisterTestingT(t) | ||
suite.Run(t, new(ContextTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: We should decide as a team if we want to use gomega or testify/{require,assert}. Not at all urgent but a bit jarring to have two different libraries depending on where we are in the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also have
gotest.tools
, so there's three now?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.. gotest.tools doesn't have all the functionality we need. e.g.: Test suites with setup and teardown functions. It also doesn't have something like testify's
require
which continues testing even if a failure is encountered.