-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add containerinfra nodegroup data source
Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@cern.ch>
- Loading branch information
1 parent
a58a8e0
commit 5f3f4d4
Showing
4 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
openstack/data_source_openstack_containerinfra_nodegroup_v1.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
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) | ||
} | ||
|
||
containerInfraClient.Microversion = containerInfraV1NodeGroupMinMicroversion | ||
|
||
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 | ||
} |
69 changes: 69 additions & 0 deletions
69
openstack/data_source_openstack_containerinfra_nodegroup_v1_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |