Skip to content

Commit

Permalink
Merge pull request #9 from avisi-cloud/add-datasources
Browse files Browse the repository at this point in the history
add datasource for node join config
  • Loading branch information
thojkooi authored Oct 26, 2023
2 parents 97f3ce6 + a92f2bc commit 7869157
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 14 deletions.
104 changes: 104 additions & 0 deletions acloud/data_source_node_join_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package acloud

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/avisi-cloud/go-client/pkg/acloudapi"
)

func dataSourceNodeJoinConfig() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNodeJoinConfigRead,
Description: "Provides access to node join configuration for a node pool. Can be used in combination with other terraform providers to provision new Kubernetes Nodes for Bring Your Own Node clusters in Avisi Cloud Kubernetes.",
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeInt,
Computed: true,
},
"organisation_slug": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the Organisation",
},
"environment_slug": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the environment of the cluster",
},
"cluster_slug": {
Type: schema.TypeString,
Required: true,
Description: "Slug of the cluster",
},
"node_pool_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the node pool",
},
"user_data": {
Type: schema.TypeString,
Computed: true,
Description: "Cloud Init user-data (base64)",
},
"kubelet_config": {
Type: schema.TypeString,
Computed: true,
},
"join_command": {
Type: schema.TypeString,
Computed: true,
},
"install_script": {
Type: schema.TypeString,
Computed: true,
Description: "Install bash script for joining a node (base64).",
},
"upgrade_script": {
Type: schema.TypeString,
Computed: true,
Description: "Install bash script for upgrading a node (base64)",
},
},
}
}

func dataSourceNodeJoinConfigRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(acloudapi.Client)

organisationSlug := d.Get("organisation_slug").(string)
environmentSlug := d.Get("environment_slug").(string)
clusterSlug := d.Get("cluster_slug").(string)

cluster, err := client.GetCluster(ctx, organisationSlug, environmentSlug, clusterSlug)
if err != nil {
return diag.FromErr(err)
}

if cluster == nil {
return diag.FromErr(fmt.Errorf("cluster was not found"))
}

nodePoolID := d.Get("node_pool_id").(string)

nodeJoinConfig, err := client.GetNodePoolJoinConfig(ctx, *cluster, acloudapi.NodePool{
Identity: nodePoolID,
})
if err != nil {
return diag.FromErr(err)
}
if nodeJoinConfig == nil {
return diag.FromErr(fmt.Errorf("node join configuration was not found"))
}

d.SetId(nodePoolID)
d.Set("user_data", nodeJoinConfig.CloudInitUserDataBase64)
d.Set("kubelet_config", nodeJoinConfig.KubeletConfigBase64)
d.Set("join_command", nodeJoinConfig.JoinCommand)
d.Set("install_script", nodeJoinConfig.InstallScriptBase64)
d.Set("upgrade_script", nodeJoinConfig.UpgradeScriptBase64)
return nil
}
16 changes: 7 additions & 9 deletions acloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func Provider() *schema.Provider {
"acloud_nodepool": resourceNodepool(),
},
DataSourcesMap: map[string]*schema.Resource{
"acloud_organisation": dataSourceOrganisations(),
"acloud_environment": dataSourceEnvironment(),
"acloud_cloud_account": dataSourceCloudAccount(),
"acloud_update_channel": dataSourceUpdateChannel(),
"acloud_organisation": dataSourceOrganisations(),
"acloud_environment": dataSourceEnvironment(),
"acloud_cloud_account": dataSourceCloudAccount(),
"acloud_update_channel": dataSourceUpdateChannel(),
"acloud_nodepool_join_config": dataSourceNodeJoinConfig(),
},
ConfigureContextFunc: providerConfigure,
}
Expand All @@ -43,18 +44,15 @@ func Provider() *schema.Provider {
func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
token := d.Get("token").(string)
acloudApiEndpoint := d.Get("acloud_api").(string)
// Warning or errors can be collected in a slice type

authenticator := acloudapi.NewPersonalAccessTokenAuthenticator(token)
clientOpts := acloudapi.ClientOpts{
APIUrl: acloudApiEndpoint,
}
var diags diag.Diagnostics
c := acloudapi.NewClient(authenticator, clientOpts)

c := acloudapi.NewClient(authenticator, clientOpts)
if token != "" {
c.Resty().OnBeforeRequest(authenticator.Authenticate)
return c, diags
}
return c, diags
return c, nil
}
3 changes: 2 additions & 1 deletion acloud/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package acloud
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -62,7 +63,7 @@ func resourceCluster() *schema.Resource {
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion acloud/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourceEnvironment() *schema.Resource {
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions acloud/resource_nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func resourceNodepool() *schema.Resource {
Type: schema.TypeString,
},
},
"taints": &schema.Schema{
"taints": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Expand All @@ -91,7 +91,7 @@ func resourceNodepool() *schema.Resource {
},
},
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
StateContext: schema.ImportStatePassthroughContext,
},
}
}
Expand Down
32 changes: 32 additions & 0 deletions docs/data-sources/nodepool_join_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "acloud_nodepool_join_config Data Source - terraform-provider-acloud"
subcategory: ""
description: |-
Provides access to node join configuration for a node pool. Can be used in combination with other terraform providers to provision new Kubernetes Nodes for Bring Your Own Node clusters in Avisi Cloud Kubernetes.
---

# acloud_nodepool_join_config (Data Source)

Provides access to node join configuration for a node pool. Can be used in combination with other terraform providers to provision new Kubernetes Nodes for Bring Your Own Node clusters in Avisi Cloud Kubernetes.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `cluster_slug` (String) Slug of the cluster
- `environment_slug` (String) Slug of the environment of the cluster
- `node_pool_id` (String) ID of the node pool
- `organisation_slug` (String) Slug of the Organisation

### Read-Only

- `id` (Number) The ID of this resource.
- `install_script` (String) Install bash script for joining a node (base64).
- `join_command` (String)
- `kubelet_config` (String)
- `upgrade_script` (String) Install bash script for upgrading a node (base64)
- `user_data` (String) Cloud Init user-data (base64)
27 changes: 27 additions & 0 deletions docs/data-sources/update_channel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "acloud_update_channel Data Source - terraform-provider-acloud"
subcategory: ""
description: |-
---

# acloud_update_channel (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the update channel
- `organisation` (String) Slug of the Organisation

### Read-Only

- `available` (Boolean) Returns if the update channel is available
- `id` (String) The ID of this resource.
- `version` (String) Avisi Cloud Kubernetes Version associated with the Update Channel
5 changes: 4 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ description: |-
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `token` (String, Sensitive)

### Optional

- `acloud_api` (String, Sensitive)
- `token` (String, Sensitive)

0 comments on commit 7869157

Please sign in to comment.