Skip to content

Commit

Permalink
Add containerinfra nodegroup resource
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@cern.ch>
  • Loading branch information
MrFreezeex committed Apr 7, 2022
1 parent a3458ea commit a58a8e0
Show file tree
Hide file tree
Showing 6 changed files with 693 additions and 2 deletions.
53 changes: 51 additions & 2 deletions openstack/containerinfra_shared_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ import (
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/certificates"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/clusters"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/clustertemplates"
"github.com/gophercloud/gophercloud/openstack/containerinfra/v1/nodegroups"
)

const (
rsaPrivateKeyBlockType = "RSA PRIVATE KEY"
certificateRequestBlockType = "CERTIFICATE REQUEST"

containerInfraV1NodeGroupMinMicroversion = "1.9"
)

func expandContainerInfraV1LabelsMap(v map[string]interface{}) (map[string]string, error) {
Expand Down Expand Up @@ -71,11 +74,57 @@ func containerInfraClusterTemplateV1AppendUpdateOpts(updateOpts []clustertemplat
return updateOpts
}

func containerInfraNodeGroupV1AppendUpdateOpts(updateOpts []nodegroups.UpdateOptsBuilder, attribute, value string) []nodegroups.UpdateOptsBuilder {
if value == "" {
updateOpts = append(updateOpts, nodegroups.UpdateOpts{
Op: nodegroups.RemoveOp,
Path: strings.Join([]string{"/", attribute}, ""),
})
} else {
updateOpts = append(updateOpts, nodegroups.UpdateOpts{
Op: nodegroups.ReplaceOp,
Path: strings.Join([]string{"/", attribute}, ""),
Value: value,
})
}
return updateOpts
}

// ContainerInfraClusterV1StateRefreshFunc returns a resource.StateRefreshFunc
// that is used to watch a container infra Cluster.
func containerInfraClusterV1StateRefreshFunc(client *gophercloud.ServiceClient, clusterID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
c, err := clusters.Get(client, clusterID).Extract()
nodeGroup, err := clusters.Get(client, clusterID).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return nodeGroup, "DELETE_COMPLETE", nil
}
return nil, "", err
}

errorStatuses := []string{
"CREATE_FAILED",
"UPDATE_FAILED",
"DELETE_FAILED",
"RESUME_FAILED",
"ROLLBACK_FAILED",
}
for _, errorStatus := range errorStatuses {
if nodeGroup.Status == errorStatus {
err = fmt.Errorf("openstack_containerinfra_cluster_v1 is in an error state: %s", nodeGroup.StatusReason)
return nodeGroup, nodeGroup.Status, err
}
}

return nodeGroup, nodeGroup.Status, nil
}
}

// ContainerInfraNodeGroupV1StateRefreshFunc returns a resource.StateRefreshFunc
// that is used to watch a container infra NodeGroup.
func containerInfraNodeGroupV1StateRefreshFunc(client *gophercloud.ServiceClient, clusterId string, nodeGroupId string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
c, err := nodegroups.Get(client, clusterId, nodeGroupId).Extract()
if err != nil {
if _, ok := err.(gophercloud.ErrDefault404); ok {
return c, "DELETE_COMPLETE", nil
Expand All @@ -92,7 +141,7 @@ func containerInfraClusterV1StateRefreshFunc(client *gophercloud.ServiceClient,
}
for _, errorStatus := range errorStatuses {
if c.Status == errorStatus {
err = fmt.Errorf("openstack_containerinfra_cluster_v1 is in an error state: %s", c.StatusReason)
err = fmt.Errorf("openstack_containerinfra_nodegroup_v1 is in an error state: %s", c.StatusReason)
return c, c.Status, err
}
}
Expand Down
38 changes: 38 additions & 0 deletions openstack/import_openstack_containerinfra_nodegroup_v1_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package openstack

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccContainerInfraV1NodeGroupImport_basic(t *testing.T) {
resourceName := "openstack_containerinfra_nodegroup_v1.nodegroup_1"
clusterName := acctest.RandomWithPrefix("tf-acc-cluster")
imageName := acctest.RandomWithPrefix("tf-acc-image")
keypairName := acctest.RandomWithPrefix("tf-acc-keypair")
clusterTemplateName := acctest.RandomWithPrefix("tf-acc-clustertemplate")
nodeGroupName := acctest.RandomWithPrefix("tf-acc-cluster")

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckContainerInfra(t)
},
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckContainerInfraV1NodeGroupDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerInfraV1NodeGroupUpdate(imageName, keypairName, clusterTemplateName, clusterName, nodeGroupName, 1),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"cluster_id"},
},
},
})
}
1 change: 1 addition & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ func Provider() *schema.Provider {
"openstack_compute_floatingip_v2": resourceComputeFloatingIPV2(),
"openstack_compute_floatingip_associate_v2": resourceComputeFloatingIPAssociateV2(),
"openstack_compute_volume_attach_v2": resourceComputeVolumeAttachV2(),
"openstack_containerinfra_nodegroup_v1": resourceContainerInfraNodeGroupV1(),
"openstack_containerinfra_clustertemplate_v1": resourceContainerInfraClusterTemplateV1(),
"openstack_containerinfra_cluster_v1": resourceContainerInfraClusterV1(),
"openstack_db_instance_v1": resourceDatabaseInstanceV1(),
Expand Down
Loading

0 comments on commit a58a8e0

Please sign in to comment.