diff --git a/openstack/data_source_openstack_containerinfra_nodegroup_v1.go b/openstack/data_source_openstack_containerinfra_nodegroup_v1.go new file mode 100644 index 000000000..80cb362f7 --- /dev/null +++ b/openstack/data_source_openstack_containerinfra_nodegroup_v1.go @@ -0,0 +1,130 @@ +package openstack + +import ( + "context" + "log" + "time" + + "github.com/gophercloud/gophercloud/openstack/containerinfra/v1/nodegroups" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +func dataSourceContainerInfraNodeGroupV1() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceContainerInfraNodeGroupRead, + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + + "cluster_id": { + Type: schema.TypeString, + Required: true, + }, + + "name": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + + "project_id": { + Type: schema.TypeString, + Computed: true, + }, + + "created_at": { + Type: schema.TypeString, + Computed: true, + }, + + "updated_at": { + Type: schema.TypeString, + Computed: true, + }, + + "docker_volume_size": { + Type: schema.TypeInt, + Computed: true, + }, + + "labels": { + Type: schema.TypeMap, + Computed: true, + }, + + "role": { + Type: schema.TypeString, + Computed: true, + }, + + "node_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "min_node_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "max_node_count": { + Type: schema.TypeInt, + Computed: true, + }, + + "image": { + Type: schema.TypeString, + Computed: true, + }, + + "flavor": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceContainerInfraNodeGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + config := meta.(*Config) + containerInfraClient, err := config.ContainerInfraV1Client(GetRegion(d, config)) + if err != nil { + return diag.Errorf("Error creating OpenStack container infra client: %s", err) + } + + clusterId := d.Get("cluster_id").(string) + name := d.Get("name").(string) + nodeGroup, err := nodegroups.Get(containerInfraClient, clusterId, name).Extract() + if err != nil { + return diag.Errorf("Error getting openstack_containerinfra_nodegroup_v1 %s: %s", name, err) + } + + d.SetId(nodeGroup.UUID) + + d.Set("project_id", nodeGroup.ProjectID) + d.Set("docker_volume_size", nodeGroup.DockerVolumeSize) + d.Set("role", nodeGroup.Role) + d.Set("node_count", nodeGroup.NodeCount) + d.Set("min_node_count", nodeGroup.MinNodeCount) + d.Set("max_node_count", nodeGroup.MaxNodeCount) + d.Set("image", nodeGroup.ImageID) + d.Set("flavor", nodeGroup.FlavorID) + + if err := d.Set("labels", nodeGroup.Labels); err != nil { + log.Printf("[DEBUG] Unable to set labels for openstack_containerinfra_nodegroup_v1 %s: %s", nodeGroup.UUID, err) + } + if err := d.Set("created_at", nodeGroup.CreatedAt.Format(time.RFC3339)); err != nil { + log.Printf("[DEBUG] Unable to set created_at for openstack_containerinfra_nodegroup_v1 %s: %s", nodeGroup.UUID, err) + } + if err := d.Set("updated_at", nodeGroup.UpdatedAt.Format(time.RFC3339)); err != nil { + log.Printf("[DEBUG] Unable to set updated_at for openstack_containerinfra_nodegroup_v1 %s: %s", nodeGroup.UUID, err) + } + + d.Set("region", GetRegion(d, config)) + + return nil +} diff --git a/openstack/data_source_openstack_containerinfra_nodegroup_v1_test.go b/openstack/data_source_openstack_containerinfra_nodegroup_v1_test.go new file mode 100644 index 000000000..8e0fc0c70 --- /dev/null +++ b/openstack/data_source_openstack_containerinfra_nodegroup_v1_test.go @@ -0,0 +1,69 @@ +package openstack + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" +) + +func TestAccContainerInfraV1NodeGroupDataSource_basic(t *testing.T) { + resourceName := "openstack_containerinfra_nodegroup_v1.nodegroup_1" + nodeGroupName := acctest.RandomWithPrefix("tf-acc-nodegroup") + clusterName := acctest.RandomWithPrefix("tf-acc-cluster") + imageName := acctest.RandomWithPrefix("tf-acc-image") + keypairName := acctest.RandomWithPrefix("tf-acc-keypair") + clusterTemplateName := acctest.RandomWithPrefix("tf-acc-clustertemplate") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckNonAdminOnly(t) + testAccPreCheckContainerInfra(t) + }, + ProviderFactories: testAccProviders, + CheckDestroy: testAccCheckContainerInfraV1ClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContainerInfraV1NodeGroupUpdate(imageName, keypairName, clusterTemplateName, clusterName, nodeGroupName, 1), + }, + { + Config: testAccContainerInfraV1NodeGroupDataSourceBasic( + testAccContainerInfraV1NodeGroupUpdate(imageName, keypairName, clusterTemplateName, clusterName, nodeGroupName, 1), + ), + Check: resource.ComposeTestCheckFunc( + testAccCheckContainerInfraV1NodeGroupDataSourceID(resourceName), + resource.TestCheckResourceAttr(resourceName, "name", nodeGroupName), + resource.TestCheckResourceAttr(resourceName, "node_count", "1"), + ), + }, + }, + }) +} + +func testAccCheckContainerInfraV1NodeGroupDataSourceID(n string) resource.TestCheckFunc { + return func(s *terraform.State) error { + ct, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Can't find cluster data source: %s", n) + } + + if ct.Primary.ID == "" { + return fmt.Errorf("Cluster data source ID is not set") + } + + return nil + } +} + +func testAccContainerInfraV1NodeGroupDataSourceBasic(clusterResource string) string { + return fmt.Sprintf(` +%s + +data "openstack_containerinfra_nodegroup_v1" "cluster_1" { + cluster_id = "${openstack_containerinfra_cluster_v1.cluster_1.name}" + name = "${openstack_containerinfra_nodegroup_v1.node_group_1.name}" +} +`, clusterResource) +} diff --git a/openstack/provider.go b/openstack/provider.go index e5445ccbd..5cd9c9cbe 100644 --- a/openstack/provider.go +++ b/openstack/provider.go @@ -270,6 +270,7 @@ func Provider() *schema.Provider { "openstack_compute_hypervisor_v2": dataSourceComputeHypervisorV2(), "openstack_compute_keypair_v2": dataSourceComputeKeypairV2(), "openstack_compute_quotaset_v2": dataSourceComputeQuotasetV2(), + "openstack_containerinfra_nodegroup_v1": dataSourceContainerInfraNodeGroupV1(), "openstack_containerinfra_clustertemplate_v1": dataSourceContainerInfraClusterTemplateV1(), "openstack_containerinfra_cluster_v1": dataSourceContainerInfraCluster(), "openstack_dns_zone_v2": dataSourceDNSZoneV2(), diff --git a/website/docs/d/containerinfra_nodegroup_v1.html.markdown b/website/docs/d/containerinfra_nodegroup_v1.html.markdown new file mode 100644 index 000000000..f995b5b3e --- /dev/null +++ b/website/docs/d/containerinfra_nodegroup_v1.html.markdown @@ -0,0 +1,64 @@ +--- +layout: "openstack" +page_title: "OpenStack: openstack_containerinfra_nodegroup_v1" +sidebar_current: "docs-openstack-datasource-containerinfra-nodegroup-v1" +description: |- + Get information on an OpenStack Magnum node group. +--- + +# openstack\_containerinfra\_nodegroup\_v1 + +Use this data source to get information of an available OpenStack Magnum node group. + +## Example Usage + +```hcl +data "openstack_containerinfra_nodegroup_v1" "nodegroup_1" { + cluster_id = "cluster_1" + name = "nodegroup_1" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional) The region in which to obtain the V1 Container Infra + client. + If omitted, the `region` argument of the provider is used. + +* `cluster_id` - (Required) The name of the OpenStack Magnum cluster. + +* `name` - (Required) The name of the node group. + +## Attributes Reference + +`id` is set to the ID of the found node group. In addition, the following +attributes are exported: + +* `name` - See Argument Reference above. + +* `region` - See Argument Reference above. + +* `project_id` - The project of the node group. + +* `created_at` - The time at which the node group was created. + +* `updated_at` - The time at which the node group was updated. + +* `docker_volume_size` - The size (in GB) of the Docker volume. + +* `labels` - The list of key value pairs representing additional properties of + the node group. + +* `role` - The role of the node group. + +* `node_count` - The number of nodes for the node group. + +* `min_node_count` - The minimum number of nodes for the node group. + +* `max_node_count` - The maximum number of nodes for the node group. + +* `image` - The reference to an image that is used for nodes of the node group. + +* `flavor` - The flavor for the nodes of the node group.