Skip to content

Commit

Permalink
Add containerinfra nodegroup data source
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 f23f7ab commit 0d5a60e
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
130 changes: 130 additions & 0 deletions openstack/data_source_openstack_containerinfra_nodegroup_v1.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
64 changes: 64 additions & 0 deletions website/docs/d/containerinfra_nodegroup_v1.html.markdown
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 0d5a60e

Please sign in to comment.