From e1a833d98931dfbd84f7f66678b42c2158f82b1a Mon Sep 17 00:00:00 2001 From: Franz Date: Mon, 11 Feb 2019 15:34:41 -0500 Subject: [PATCH 1/4] Added dedicated_group_id field --- cloudca/resource_cloudca_instance.go | 33 ++++++++++ go.mod | 2 +- go.sum | 2 + .../services/cloudca/affinity_groups.go | 65 +++++++++++++++++++ .../services/cloudca/entity_type.go | 1 + .../go-cloudca/services/cloudca/instance.go | 2 + .../go-cloudca/services/cloudca/resources.go | 2 + vendor/modules.txt | 2 +- 8 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 vendor/github.com/cloud-ca/go-cloudca/services/cloudca/affinity_groups.go diff --git a/cloudca/resource_cloudca_instance.go b/cloudca/resource_cloudca_instance.go index ff66d569..4371dd65 100644 --- a/cloudca/resource_cloudca_instance.go +++ b/cloudca/resource_cloudca_instance.go @@ -105,6 +105,12 @@ func resourceCloudcaInstance() *schema.Resource { Computed: true, Description: "The IPv4 address of the instance. Must be within the network's CIDR and not collide with existing instances.", }, + "dedicated_group_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Description: "Id of the dedicate group into which the new instance will be created", + }, }, } } @@ -168,6 +174,10 @@ func resourceCloudcaInstanceCreate(d *schema.ResourceData, meta interface{}) err instanceToCreate.RootVolumeSizeInGb = rootVolumeSizeInGb.(int) } + if dedicatedGroupID, ok := d.GetOk("dedicated_group_id"); ok { + instanceToCreate.DedicatedGroupId = dedicatedGroupID.(string) + } + newInstance, err := ccaResources.Instances.Create(instanceToCreate) if err != nil { return fmt.Errorf("Error creating the new instance %s: %s", instanceToCreate.Name, err) @@ -208,6 +218,11 @@ func resourceCloudcaInstanceRead(d *schema.ResourceData, meta interface{}) error d.Set("network_id", instance.NetworkId) d.Set("private_ip_id", instance.IpAddressId) d.Set("private_ip", instance.IpAddress) + dID, dIDErr := getDedicatedGroupId(ccaResources, instance) + if dIDErr != nil { + return dIDErr + } + d.Set("dedicated_group_id", dID) return nil } @@ -334,3 +349,21 @@ func retrieveTemplateID(ccaRes *cloudca.Resources, name string) (id string, err return "", fmt.Errorf("Template with name %s not found", name) } + + +func getDedicatedGroupId(ccaRes cloudca.Resources, instance *cloudca.Instance) (string, error) { + dedicatedGroups, err := ccaRes.AffinityGroups.ListWithOptions(map[string]string{ + "type": "ExplicitDedication", + }) + if err != nil { + return "", err + } + for _, dedicatedGroup := range dedicatedGroups { + for _, affinityGroupId := range instance.AffinityGroupIds { + if strings.EqualFold(dedicatedGroup.Id, affinityGroupId) { + return dedicatedGroup.Id, nil + } + } + } + return "", nil +} \ No newline at end of file diff --git a/go.mod b/go.mod index 6ef1705a..e460dbb4 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cloud-ca/terraform-provider-cloudca require ( github.com/apparentlymart/go-cidr v1.0.0 // indirect github.com/blang/semver v3.5.1+incompatible // indirect - github.com/cloud-ca/go-cloudca v1.2.0 + github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528 github.com/hashicorp/go-getter v1.0.3 // indirect github.com/hashicorp/go-hclog v0.0.0-20190109152822-4783caec6f2e // indirect github.com/hashicorp/go-plugin v0.0.0-20190129155509-362c99b11937 // indirect diff --git a/go.sum b/go.sum index 4d91ba18..6ed59024 100644 --- a/go.sum +++ b/go.sum @@ -19,6 +19,8 @@ github.com/bsm/go-vlq v0.0.0-20150828105119-ec6e8d4f5f4e/go.mod h1:N+BjUcTjSxc2m github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXHm81s= github.com/cloud-ca/go-cloudca v1.2.0 h1:215x34c2XUMxj9GV5gYH9JhqtabDysX2quXvNiKIgco= github.com/cloud-ca/go-cloudca v1.2.0/go.mod h1:L8yDVtO6lmn2H50a6nMxYcGINgU6kVwpDeF/IxZVpsg= +github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528 h1:DPBpPNdP0SD6InpeChFXPksAPK+3UQFQNY6vBua0QPc= +github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528/go.mod h1:L8yDVtO6lmn2H50a6nMxYcGINgU6kVwpDeF/IxZVpsg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= diff --git a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/affinity_groups.go b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/affinity_groups.go new file mode 100644 index 00000000..6f3f50e2 --- /dev/null +++ b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/affinity_groups.go @@ -0,0 +1,65 @@ +package cloudca + +import ( + "encoding/json" + "github.com/cloud-ca/go-cloudca/api" + "github.com/cloud-ca/go-cloudca/services" +) + +type AffinityGroup struct { + Id string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + Type string `json:"type,omitempty"` + InstanceIds []string `json:"instanceIds,omitempty"` + InstanceNames []string `json:"instanceNames,omitempty"` + ZoneIds []string `json:"zoneIds,omitempty"` +} + +type AffinityGroupService interface { + Get(string) (*AffinityGroup, error) + List() ([]AffinityGroup, error) + ListWithOptions(map[string]string) ([]AffinityGroup, error) +} + +type AffinityGroupApi struct { + entityService services.EntityService +} + +func NewAffinityGroupsService(apiClient api.ApiClient, serviceCode string, environmentName string) AffinityGroupService { + return &AffinityGroupApi{ + entityService: services.NewEntityService(apiClient, serviceCode, environmentName, AFFINITY_GROUP_ENTITY_TYPE), + } +} + +func parseAffinityGroup(data []byte) *AffinityGroup { + affinityGroup := AffinityGroup{} + json.Unmarshal(data, &affinityGroup) + return &affinityGroup +} + +func parseAffinityGroupList(data []byte) []AffinityGroup { + affinityGroups := []AffinityGroup{} + json.Unmarshal(data, &affinityGroups) + return affinityGroups +} + +func (api *AffinityGroupApi) Get(id string) (*AffinityGroup, error) { + resp, err := api.entityService.Get(id, map[string]string{}) + if err != nil { + return nil, err + } + return parseAffinityGroup(resp), nil +} + +func (api *AffinityGroupApi) List() ([]AffinityGroup, error) { + return api.ListWithOptions(map[string]string{}) +} + +func (api *AffinityGroupApi) ListWithOptions(options map[string]string) ([]AffinityGroup, error) { + resp, err := api.entityService.List(options) + if err != nil { + return nil, err + } + return parseAffinityGroupList(resp), nil +} diff --git a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/entity_type.go b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/entity_type.go index 8778d175..c94e1524 100644 --- a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/entity_type.go +++ b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/entity_type.go @@ -2,6 +2,7 @@ package cloudca //All entity types for the cloudca service const ( + AFFINITY_GROUP_ENTITY_TYPE = "affinitygroups" INSTANCE_ENTITY_TYPE = "instances" VOLUME_ENTITY_TYPE = "volumes" TEMPLATE_ENTITY_TYPE = "templates" diff --git a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/instance.go b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/instance.go index f5782f6c..4edd61dc 100644 --- a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/instance.go +++ b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/instance.go @@ -61,6 +61,8 @@ type Instance struct { VolumeIdToAttach string `json:"volumeIdToAttach,omitempty"` PortsToForward []string `json:"portsToForward,omitempty"` RootVolumeSizeInGb int `json:"rootVolumeSizeInGb,omitempty"` + DedicatedGroupId string `json:"dedicatedGroupId,omitempty"` + AffinityGroupIds []string `json:"affinityGroupIds,omitempty"` } type DestroyOptions struct { diff --git a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/resources.go b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/resources.go index d5ce01aa..ebbb971b 100644 --- a/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/resources.go +++ b/vendor/github.com/cloud-ca/go-cloudca/services/cloudca/resources.go @@ -12,6 +12,7 @@ type Resources struct { apiClient api.ApiClient serviceCode string environmentName string + AffinityGroups AffinityGroupService Instances InstanceService Volumes VolumeService Templates TemplateService @@ -35,6 +36,7 @@ func NewResources(apiClient api.ApiClient, serviceCode string, environmentName s apiClient: apiClient, serviceCode: serviceCode, environmentName: environmentName, + AffinityGroups: NewAffinityGroupsService(apiClient, serviceCode, environmentName), Instances: NewInstanceService(apiClient, serviceCode, environmentName), Volumes: NewVolumeService(apiClient, serviceCode, environmentName), Templates: NewTemplateService(apiClient, serviceCode, environmentName), diff --git a/vendor/modules.txt b/vendor/modules.txt index 9fda1d7b..52572c9a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -46,7 +46,7 @@ github.com/bgentry/go-netrc/netrc github.com/bgentry/speakeasy # github.com/blang/semver v3.5.1+incompatible github.com/blang/semver -# github.com/cloud-ca/go-cloudca v1.2.0 +# github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528 github.com/cloud-ca/go-cloudca github.com/cloud-ca/go-cloudca/api github.com/cloud-ca/go-cloudca/configuration From 9c0b12899e0e87755267f55f1491c4ad0ba6c101 Mon Sep 17 00:00:00 2001 From: Franz Date: Mon, 11 Feb 2019 15:40:19 -0500 Subject: [PATCH 2/4] Added doc --- doc/instance.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/instance.md b/doc/instance.md index 0552bbc0..92729467 100644 --- a/doc/instance.md +++ b/doc/instance.md @@ -14,6 +14,7 @@ resource "cloudca_instance" "my_instance" { ssh_key_name = "my_ssh_key" root_volume_size_in_gb = 100 private_ip = "10.2.1.124" + dedicated_group_id = "78fdce97-3a46-4b50-bca7-c70ef8449da8" } ``` @@ -33,6 +34,7 @@ The following arguments are supported: - [public_key](#public_key) - (Optional) Public key to attach to the instance. Mutually exclusive with ssh_key_name. - [root_volume_size_in_gb](#root_volume_size_in_gb) - (Optional) Size of the root volume of the instance. This only works for templates that allows root volume resize. - [private_ip](#private_ip) - (Optional) Instance's private IPv4 address. +- [dedicated_group_id](#dedicated_group_id) - (Optional) Dedicated group id in which the instance will be created ## Attribute Reference From 6c9a7bbdbfc01a822933774144930f6786b481b2 Mon Sep 17 00:00:00 2001 From: Franz Date: Mon, 11 Feb 2019 15:57:00 -0500 Subject: [PATCH 3/4] Change go cloud-ca to 1.3.0 --- go.mod | 2 +- go.sum | 2 ++ vendor/modules.txt | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e460dbb4..3f1c8965 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/cloud-ca/terraform-provider-cloudca require ( github.com/apparentlymart/go-cidr v1.0.0 // indirect github.com/blang/semver v3.5.1+incompatible // indirect - github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528 + github.com/cloud-ca/go-cloudca v1.3.0 github.com/hashicorp/go-getter v1.0.3 // indirect github.com/hashicorp/go-hclog v0.0.0-20190109152822-4783caec6f2e // indirect github.com/hashicorp/go-plugin v0.0.0-20190129155509-362c99b11937 // indirect diff --git a/go.sum b/go.sum index 6ed59024..37173f3b 100644 --- a/go.sum +++ b/go.sum @@ -21,6 +21,8 @@ github.com/cloud-ca/go-cloudca v1.2.0 h1:215x34c2XUMxj9GV5gYH9JhqtabDysX2quXvNiK github.com/cloud-ca/go-cloudca v1.2.0/go.mod h1:L8yDVtO6lmn2H50a6nMxYcGINgU6kVwpDeF/IxZVpsg= github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528 h1:DPBpPNdP0SD6InpeChFXPksAPK+3UQFQNY6vBua0QPc= github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528/go.mod h1:L8yDVtO6lmn2H50a6nMxYcGINgU6kVwpDeF/IxZVpsg= +github.com/cloud-ca/go-cloudca v1.3.0 h1:LhoCxu+Tna/OOoDbSD2adgwlA+WgKfUzzcHBcEu5akE= +github.com/cloud-ca/go-cloudca v1.3.0/go.mod h1:L8yDVtO6lmn2H50a6nMxYcGINgU6kVwpDeF/IxZVpsg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys= diff --git a/vendor/modules.txt b/vendor/modules.txt index 52572c9a..3472e111 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -46,7 +46,7 @@ github.com/bgentry/go-netrc/netrc github.com/bgentry/speakeasy # github.com/blang/semver v3.5.1+incompatible github.com/blang/semver -# github.com/cloud-ca/go-cloudca v1.2.1-0.20190211191729-cd925097c528 +# github.com/cloud-ca/go-cloudca v1.3.0 github.com/cloud-ca/go-cloudca github.com/cloud-ca/go-cloudca/api github.com/cloud-ca/go-cloudca/configuration From 0ed3204ebde00ccb9ea2c719b6a218cb59656883 Mon Sep 17 00:00:00 2001 From: Franz Date: Mon, 11 Feb 2019 16:27:16 -0500 Subject: [PATCH 4/4] Fixed typo --- cloudca/resource_cloudca_instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cloudca/resource_cloudca_instance.go b/cloudca/resource_cloudca_instance.go index 4371dd65..435b8547 100644 --- a/cloudca/resource_cloudca_instance.go +++ b/cloudca/resource_cloudca_instance.go @@ -109,7 +109,7 @@ func resourceCloudcaInstance() *schema.Resource { Type: schema.TypeString, Optional: true, ForceNew: true, - Description: "Id of the dedicate group into which the new instance will be created", + Description: "Id of the dedicated group into which the new instance will be created", }, }, }