-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gridscale_k8s data source and documentation (#425)
* Add gridscale_k8s data source and documentation * refactored gridscale_k8s tests to include additional attribute checks and update configuration for tested resource * fix: add missing website content
- Loading branch information
Showing
5 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
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,111 @@ | ||
package gridscale | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gridscale/gsclient-go/v3" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func dataSourceGridscaleK8s() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGridscaleK8sRead, | ||
Schema: map[string]*schema.Schema{ | ||
"resource_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "ID of a resource", | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "The human-readable name of the object. It supports the full UTF-8 character set, with a maximum of 64 characters", | ||
Computed: true, | ||
}, | ||
"kubeconfig": { | ||
Type: schema.TypeString, | ||
Description: "K8s config data", | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"k8s_private_network_uuid": { | ||
Type: schema.TypeString, | ||
Description: "Private network UUID which k8s nodes are attached to. It can be used to attach other PaaS/VMs.", | ||
Computed: true, | ||
}, | ||
"labels": { | ||
Type: schema.TypeSet, | ||
Description: "List of labels.", | ||
Optional: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGridscaleK8sRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*gsclient.Client) | ||
id := d.Get("resource_id").(string) | ||
d.SetId(id) | ||
errorPrefix := fmt.Sprintf("read k8s (%s) resource -", id) | ||
paas, err := client.GetPaaSService(context.Background(), id) | ||
|
||
if err != nil { | ||
if requestError, ok := err.(gsclient.RequestError); ok { | ||
if requestError.StatusCode == 404 { | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
return fmt.Errorf("%s error: %v", errorPrefix, err) | ||
} | ||
creds := paas.Properties.Credentials | ||
if len(creds) > 0 { | ||
// if expiration_time of kubeconfig is reached, renew it and get new kubeconfig | ||
if creds[0].ExpirationTime.Before(time.Now()) { | ||
err = client.RenewK8sCredentials(context.Background(), d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("%s error renewing k8s kubeconfig: %v", errorPrefix, err) | ||
} | ||
paas, err = client.GetPaaSService(context.Background(), d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("%s error: %v", errorPrefix, err) | ||
} | ||
creds = paas.Properties.Credentials | ||
} | ||
if err = d.Set("kubeconfig", creds[0].KubeConfig); err != nil { | ||
return fmt.Errorf("%s error setting kubeconfig: %v", errorPrefix, err) | ||
} | ||
} | ||
|
||
if err = d.Set("name", paas.Properties.Name); err != nil { | ||
return fmt.Errorf("%s error setting name: %v", errorPrefix, err) | ||
} | ||
if err = d.Set("kubeconfig", creds[0].KubeConfig); err != nil { | ||
return fmt.Errorf("%s error setting name: %v", errorPrefix, err) | ||
} | ||
if err = d.Set("labels", paas.Properties.Labels); err != nil { | ||
return fmt.Errorf("%s error setting labels: %v", errorPrefix, err) | ||
} | ||
networks, err := client.GetNetworkList(context.Background()) | ||
if err != nil { | ||
return fmt.Errorf("%s error getting networks: %v", errorPrefix, err) | ||
} | ||
// find the network with the label that matches the k8s label | ||
k8sLabel := fmt.Sprintf("%s%s", k8sLabelPrefix, d.Id()) | ||
NETWORK_LOOOP: | ||
for _, network := range networks { | ||
for _, label := range network.Properties.Labels { | ||
if label == k8sLabel { | ||
if err = d.Set("k8s_private_network_uuid", network.Properties.ObjectUUID); err != nil { | ||
return fmt.Errorf("%s error setting k8s_private_network_uuid: %v", errorPrefix, err) | ||
} | ||
break NETWORK_LOOOP | ||
} | ||
} | ||
} | ||
return nil | ||
} |
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,53 @@ | ||
package gridscale | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceGridscaleK8sBasic(t *testing.T) { | ||
name := fmt.Sprintf("object-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckResourceGridscaleServerDestroyCheck, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckDataSourceGridscaleK8sConfigBasic(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet( | ||
"data.gridscale_k8s.test", "k8s_private_network_uuid"), | ||
resource.TestCheckResourceAttrSet( | ||
"data.gridscale_k8s.test", "kubeconfig"), | ||
resource.TestCheckResourceAttrSet( | ||
"data.gridscale_k8s.test", "labels"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckDataSourceGridscaleK8sConfigBasic(name string) string { | ||
return fmt.Sprintf(` | ||
resource "gridscale_k8s" "test" { | ||
name = "%s" | ||
release = "1.30" # instead, gsk_version can be set. | ||
node_pool { | ||
name = "pool-0" | ||
node_count = 2 | ||
cores = 2 | ||
memory = 4 | ||
storage = 30 | ||
storage_type = "storage_insane" | ||
} | ||
} | ||
data "gridscale_k8s" "test" { | ||
resource_id = gridscale_k8s.test.id | ||
}`, name) | ||
} |
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,44 @@ | ||
--- | ||
layout: "gridscale" | ||
page_title: "gridscale: gridscale_k8s" | ||
sidebar_current: "docs-gridscale-resource-k8s" | ||
description: |- | ||
Get data from a k8s cluster in gridscale. | ||
--- | ||
|
||
# gridscale_k8s | ||
|
||
|
||
Get information about a Gridscale Kubernetes cluster. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "gridscale_k8s" "k8s-example" { | ||
resource_id = "xxxx-xxxx-xxxx-xxxx" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `resource_id` - (Required) The UUID of the Kubernetes cluster. | ||
|
||
## Timeouts | ||
|
||
Timeouts configuration options (in seconds): | ||
More info: [terraform.io/docs/configuration/resources.html#operation-timeouts](https://www.terraform.io/docs/configuration/resources.html#operation-timeouts) | ||
|
||
* `create` - (Default value is "45m" - 45 minutes) Used for creating a resource. | ||
* `update` - (Default value is "45m" - 45 minutes) Used for updating a resource. | ||
* `delete` - (Default value is "45m" - 45 minutes) Used for deleting a resource. | ||
|
||
## Attributes | ||
|
||
This resource exports the following attributes: | ||
|
||
* `name` - The human-readable name of the Kubernetes cluster. | ||
* `labels` - See Argument Reference above. | ||
* `kubeconfig` - The kubeconfig file content of the k8s cluster. | ||
* `k8s_private_network_uuid` - Private network UUID which k8s nodes are attached to. It can be used to attach other PaaS/VMs. |
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