-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
298 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cloudinit | ||
|
||
const ( | ||
// InstanceDataKey is the metdata key name to use for instance data. | ||
InstanceDataKey = "meta-data" | ||
// UserdataKey is the metadata key name to use for user data. | ||
UserdataKey = "user-data" | ||
// VendorDataKey is the metadata key name to use for vendor data. | ||
VendorDataKey = "vendor-data" | ||
) |
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,19 @@ | ||
package instance | ||
|
||
// These constants represent standard instance metadata key names. | ||
const ( | ||
// CloudNameKey is the instance metdata key name representing the cloud name. | ||
CloudNameKey = "cloud_name" | ||
// InstanceIDKey is the instance metdata key name representing the unique instance id mof the instance. | ||
InstanceIDKey = "instance_id" | ||
// LocalHostnameKey is the instance metdata key name representing the host name of the instance. | ||
LocalHostnameKey = "local_hostname" | ||
// PlatformKey is the instance metdata key name representing the hosting platform of the instance. | ||
PlatformKey = "platform" | ||
) | ||
|
||
// These constants represents custom instance metadata names | ||
const ( | ||
// ClusterNameKey is the instance metdata key name representing the cluster name of the instance. | ||
ClusterNameKey = "cluster_name" | ||
) |
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,73 @@ | ||
package instance | ||
|
||
// New creates a new instance metadata | ||
func New(opts ...MetadataOption) Metadata { | ||
m := map[string]string{} | ||
|
||
for _, opt := range opts { | ||
opt(m) | ||
} | ||
|
||
return m | ||
} | ||
|
||
// Metadata represents the cloud-init instance metadata. | ||
// See https://cloudinit.readthedocs.io/en/latest/topics/instancedata.html | ||
type Metadata map[string]string | ||
|
||
// HasItem returns true/false if the specific metadata item exists. | ||
func (m Metadata) HasItem(name string) bool { | ||
if len(m) == 0 { | ||
return false | ||
} | ||
_, ok := m[name] | ||
|
||
return ok | ||
} | ||
|
||
// MetadataOption is an option when creating an instance of Metadata | ||
type MetadataOption func(Metadata) | ||
|
||
// WithInstanceID will set the instance id metadata. | ||
func WithInstanceID(instanceID string) MetadataOption { | ||
return func(im Metadata) { | ||
im[InstanceIDKey] = instanceID | ||
} | ||
} | ||
|
||
// WithCloudName will set the cloud name metadata. | ||
func WithCloudName(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[CloudNameKey] = name | ||
} | ||
} | ||
|
||
// WithLocalHostname will set the local hostname metadata. | ||
func WithLocalHostname(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[LocalHostnameKey] = name | ||
} | ||
} | ||
|
||
// WithPlatform will set the platform metadata. | ||
func WithPlatform(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[PlatformKey] = name | ||
} | ||
} | ||
|
||
// WithClusterName will set the cluster name metadata. | ||
func WithClusterName(name string) MetadataOption { | ||
return func(im Metadata) { | ||
im[ClusterNameKey] = name | ||
} | ||
} | ||
|
||
// WithExisting will set the metadata keys/values based on an existing Metadata. | ||
func WithExisting(existing Metadata) MetadataOption { | ||
return func(im Metadata) { | ||
for k, v := range existing { | ||
im[k] = v | ||
} | ||
} | ||
} |
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,71 @@ | ||
package instance_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
|
||
"github.com/weaveworks/flintlock/client/cloudinit/instance" | ||
) | ||
|
||
const ( | ||
testCloudName = "nocloud" | ||
testClusterName = "cluster1" | ||
testInstanceID = "i-123456" | ||
testHostName = "host1" | ||
testPlatformName = "liquidmetal" | ||
) | ||
|
||
func TestInstanceMetadata_NewNoOptions(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
m := instance.New() | ||
Expect(m).NotTo(BeNil()) | ||
Expect(m).To(HaveLen(0)) | ||
} | ||
|
||
func TestInstanceMetadata_NewWithOptions(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
m := instance.New( | ||
instance.WithCloudName(testCloudName), | ||
instance.WithClusterName(testClusterName), | ||
instance.WithInstanceID(testInstanceID), | ||
instance.WithLocalHostname(testHostName), | ||
instance.WithPlatform(testPlatformName), | ||
) | ||
Expect(m).NotTo(BeNil()) | ||
Expect(m).To(HaveLen(5)) | ||
|
||
Expect(m[instance.CloudNameKey]).To(Equal(testCloudName)) | ||
Expect(m[instance.ClusterNameKey]).To(Equal(testClusterName)) | ||
Expect(m[instance.InstanceIDKey]).To(Equal(testInstanceID)) | ||
Expect(m[instance.LocalHostnameKey]).To(Equal(testHostName)) | ||
Expect(m[instance.PlatformKey]).To(Equal(testPlatformName)) | ||
} | ||
|
||
func TestInstanceMetadata_NewOptionsExisting(t *testing.T) { | ||
RegisterTestingT(t) | ||
|
||
existing := instance.New( | ||
instance.WithCloudName(testCloudName), | ||
instance.WithClusterName(testClusterName), | ||
instance.WithInstanceID(testInstanceID), | ||
instance.WithLocalHostname(testHostName), | ||
instance.WithPlatform(testPlatformName), | ||
) | ||
|
||
m := instance.New( | ||
instance.WithExisting(existing), | ||
instance.WithInstanceID("changed"), | ||
) | ||
|
||
Expect(m).NotTo(BeNil()) | ||
Expect(m).To(HaveLen(5)) | ||
|
||
Expect(m[instance.CloudNameKey]).To(Equal(testCloudName)) | ||
Expect(m[instance.ClusterNameKey]).To(Equal(testClusterName)) | ||
Expect(m[instance.InstanceIDKey]).To(Equal("changed")) | ||
Expect(m[instance.LocalHostnameKey]).To(Equal(testHostName)) | ||
Expect(m[instance.PlatformKey]).To(Equal(testPlatformName)) | ||
} |
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
client/cloudinit/network.go → client/cloudinit/network/network.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cloudinit | ||
package network | ||
|
||
type Network struct { | ||
Version int `yaml:"version"` | ||
|
2 changes: 1 addition & 1 deletion
2
client/cloudinit/userdata.go → client/cloudinit/userdata/userdata.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package cloudinit | ||
package userdata | ||
|
||
type UserData struct { | ||
HostName string `yaml:"hostname,omitempty"` | ||
|
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
Oops, something went wrong.