mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-02-02 18:44:49 -05:00
Create general cloudtypes instances
This commit is contained in:
parent
4912ab0195
commit
f9b471e3c0
58
cli/cloud/cloudtypes/instance.go
Normal file
58
cli/cloud/cloudtypes/instance.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package cloudtypes
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
// Instance is a gcp instance.
|
||||||
|
type Instance struct {
|
||||||
|
PublicIP string
|
||||||
|
PrivateIP string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Instances is a map of gcp Instances. The ID of an instance is used as key.
|
||||||
|
type Instances map[string]Instance
|
||||||
|
|
||||||
|
// IDs returns the IDs of all instances of the Constellation.
|
||||||
|
func (i Instances) IDs() []string {
|
||||||
|
var ids []string
|
||||||
|
for id := range i {
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
// PublicIPs returns the public IPs of all the instances of the Constellation.
|
||||||
|
func (i Instances) PublicIPs() []string {
|
||||||
|
var ips []string
|
||||||
|
for _, instance := range i {
|
||||||
|
ips = append(ips, instance.PublicIP)
|
||||||
|
}
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrivateIPs returns the private IPs of all the instances of the Constellation.
|
||||||
|
func (i Instances) PrivateIPs() []string {
|
||||||
|
var ips []string
|
||||||
|
for _, instance := range i {
|
||||||
|
ips = append(ips, instance.PrivateIP)
|
||||||
|
}
|
||||||
|
return ips
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOne return anyone instance out of the instances and its ID.
|
||||||
|
func (i Instances) GetOne() (string, Instance, error) {
|
||||||
|
for id, instance := range i {
|
||||||
|
return id, instance, nil
|
||||||
|
}
|
||||||
|
return "", Instance{}, errors.New("map is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetOthers returns all instances but the one with the handed ID.
|
||||||
|
func (i Instances) GetOthers(id string) Instances {
|
||||||
|
others := make(Instances)
|
||||||
|
for key, instance := range i {
|
||||||
|
if key != id {
|
||||||
|
others[key] = instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return others
|
||||||
|
}
|
71
cli/cloud/cloudtypes/instance_test.go
Normal file
71
cli/cloud/cloudtypes/instance_test.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package cloudtypes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestIDs(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
testState := testInstances()
|
||||||
|
wantIDs := []string{"id-9", "id-10", "id-11", "id-12"}
|
||||||
|
assert.ElementsMatch(wantIDs, testState.IDs())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPublicIPs(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
testState := testInstances()
|
||||||
|
wantIPs := []string{"192.0.2.1", "192.0.2.3", "192.0.2.5", "192.0.2.7"}
|
||||||
|
assert.ElementsMatch(wantIPs, testState.PublicIPs())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrivateIPs(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
testState := testInstances()
|
||||||
|
wantIPs := []string{"192.0.2.2", "192.0.2.4", "192.0.2.6", "192.0.2.8"}
|
||||||
|
assert.ElementsMatch(wantIPs, testState.PrivateIPs())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetOne(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
testState := testInstances()
|
||||||
|
id, instance, err := testState.GetOne()
|
||||||
|
assert.NoError(err)
|
||||||
|
assert.Contains(testState, id)
|
||||||
|
assert.Equal(testState[id], instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetOthers(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
testCases := testInstances().IDs()
|
||||||
|
|
||||||
|
for _, id := range testCases {
|
||||||
|
others := testInstances().GetOthers(id)
|
||||||
|
assert.NotContains(others, id)
|
||||||
|
wantInstances := testInstances()
|
||||||
|
delete(wantInstances, id)
|
||||||
|
assert.ElementsMatch(others.IDs(), wantInstances.IDs())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testInstances() Instances {
|
||||||
|
return Instances{
|
||||||
|
"id-9": {
|
||||||
|
PublicIP: "192.0.2.1",
|
||||||
|
PrivateIP: "192.0.2.2",
|
||||||
|
},
|
||||||
|
"id-10": {
|
||||||
|
PublicIP: "192.0.2.3",
|
||||||
|
PrivateIP: "192.0.2.4",
|
||||||
|
},
|
||||||
|
"id-11": {
|
||||||
|
PublicIP: "192.0.2.5",
|
||||||
|
PrivateIP: "192.0.2.6",
|
||||||
|
},
|
||||||
|
"id-12": {
|
||||||
|
PublicIP: "192.0.2.7",
|
||||||
|
PrivateIP: "192.0.2.8",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user