Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
Added kube_config computed field on resource cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
rawmind0 authored and cloudnautique committed Mar 6, 2019
1 parent e4f8a85 commit 2618ed9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
59 changes: 29 additions & 30 deletions rancher2/resource_rancher2_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
managementClient "github.com/rancher/types/client/management/v3"
//"gopkg.in/yaml.v2"
)

const (
Expand All @@ -34,6 +33,10 @@ func clusterFields() map[string]*schema.Schema {
Default: clusterRkeKind,
ValidateFunc: validation.StringInSlice(clusterKinds, true),
},
"kube_config": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"rke_config": &schema.Schema{
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -99,7 +102,7 @@ func clusterFields() map[string]*schema.Schema {

// Flatteners

func flattenCluster(d *schema.ResourceData, in *managementClient.Cluster, clusterRegToken *managementClient.ClusterRegistrationToken) error {
func flattenCluster(d *schema.ResourceData, in *managementClient.Cluster, clusterRegToken *managementClient.ClusterRegistrationToken, kubeConfig *managementClient.GenerateKubeConfigOutput) error {
if in == nil {
return fmt.Errorf("[ERROR] flattening cluster: Input cluster is nil")
}
Expand All @@ -108,7 +111,14 @@ func flattenCluster(d *schema.ResourceData, in *managementClient.Cluster, cluste
return fmt.Errorf("[ERROR] flattening cluster: Input cluster registration token is nil")
}

d.SetId(in.ID)
if kubeConfig == nil {
return fmt.Errorf("[ERROR] flattening cluster: Input cluster kube config is nil")
}

if in.ID != "" {
d.SetId(in.ID)
}

err := d.Set("name", in.Name)
if err != nil {
return err
Expand All @@ -134,6 +144,11 @@ func flattenCluster(d *schema.ResourceData, in *managementClient.Cluster, cluste
return err
}

err = d.Set("kube_config", kubeConfig.Config)
if err != nil {
return err
}

kind := d.Get("kind").(string)
if kind == "" {
if in.AzureKubernetesServiceConfig != nil {
Expand Down Expand Up @@ -315,16 +330,7 @@ func resourceRancher2ClusterCreate(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("[ERROR] waiting for cluster (%s) to be created: %s", newCluster.ID, waitErr)
}

clusterRegistrationToken, err := findClusterRegistrationToken(client, newCluster.ID)
if err != nil {
return err
}

d.SetId(newCluster.ID)
err = flattenCluster(d, newCluster, clusterRegistrationToken)
if err != nil {
return err
}

return resourceRancher2ClusterRead(d, meta)
}
Expand All @@ -340,19 +346,24 @@ func resourceRancher2ClusterRead(d *schema.ResourceData, meta interface{}) error
cluster, err := client.Cluster.ByID(d.Id())
if err != nil {
if IsNotFound(err) {
log.Printf("[INFO] Cluster ID %s not found.", d.Id())
log.Printf("[INFO] Cluster ID %s not found.", cluster.ID)
d.SetId("")
return nil
}
return err
}

clusterRegistrationToken, err := findClusterRegistrationToken(client, d.Id())
clusterRegistrationToken, err := findClusterRegistrationToken(client, cluster.ID)
if err != nil {
return err
}

err = flattenCluster(d, cluster, clusterRegistrationToken)
kubeConfig, err := client.Cluster.ActionGenerateKubeconfig(cluster)
if err != nil {
return err
}

err = flattenCluster(d, cluster, clusterRegistrationToken, kubeConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -425,6 +436,8 @@ func resourceRancher2ClusterUpdate(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("[ERROR] waiting for cluster (%s) to be updated: %s", newCluster.ID, waitErr)
}

d.SetId(newCluster.ID)

return resourceRancher2ClusterRead(d, meta)
}

Expand Down Expand Up @@ -473,21 +486,7 @@ func resourceRancher2ClusterDelete(d *schema.ResourceData, meta interface{}) err
}

func resourceRancher2ClusterImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return []*schema.ResourceData{}, err
}
cluster, err := client.Cluster.ByID(d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}

clusterRegistrationToken, err := findClusterRegistrationToken(client, d.Id())
if err != nil {
return []*schema.ResourceData{}, err
}

err = flattenCluster(d, cluster, clusterRegistrationToken)
err := resourceRancher2ClusterRead(d, meta)
if err != nil {
return []*schema.ResourceData{}, err
}
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@ The following arguments are supported:
The following attributes are exported:

* `id` - (Computed) The ID of the resource.
* `cluster_registration_token` - (Computed) The Cluster Registration Token generated for Clusters.
* `cluster_registration_token` - (Computed) Cluster Registration Token generated for the cluster.
* `kube_config` - (Computed) Kube Config generated for the cluster.

The following attributes are exported for `cluster_registration_token`:

Expand Down

0 comments on commit 2618ed9

Please sign in to comment.