Skip to content

Commit

Permalink
delete 'credential' field, add type safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
furkhat committed Feb 4, 2021
1 parent f44154d commit f473e25
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 184 deletions.
2 changes: 0 additions & 2 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ resource "metakube_cluster" "example" {
project_id = metakube_project.example.id
name = "example"
dc_name = "europe-west3-c"
credential = ""
spec {
version = "1.18.8"
Expand Down Expand Up @@ -41,7 +40,6 @@ The following arguments are supported:
* `spec` - (Required) Cluster specification.
* `labels` - (Optional) Labels added to cluster.
* `sshkeys` - (Optional) SSH keys attached to nodes.
* `credential` - (Optional) Cluster access credentials.
* `type` - (Optional) Cloud orchestrator, either Kubernetes or OpenShift.

## Attributes
Expand Down
26 changes: 7 additions & 19 deletions metakube/resource_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package metakube
import (
"context"
"fmt"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"net/http"
"strings"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -81,12 +82,6 @@ func resourceCluster() *schema.Resource {
},
},

"credential": {
Type: schema.TypeString,
Optional: true,
Description: "Cluster access credential",
},

"type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -152,11 +147,10 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, m interf
clusterSpec := expandClusterSpec(d.Get("spec").([]interface{}), d.Get("dc_name").(string))
createClusterSpec := &models.CreateClusterSpec{
Cluster: &models.Cluster{
Name: d.Get("name").(string),
Spec: clusterSpec,
Type: d.Get("type").(string),
Labels: getLabels(d),
Credential: d.Get("credential").(string),
Name: d.Get("name").(string),
Spec: clusterSpec,
Type: d.Get("type").(string),
Labels: getLabels(d),
},
}
if n := clusterSpec.ClusterNetwork; n != nil {
Expand Down Expand Up @@ -341,12 +335,6 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, m interfac

_ = d.Set("name", r.Payload.Name)

// TODO: check why API returns an empty credential field even if it is set
//err = d.Set("credential", r.Payload.Credential)
//if err != nil {
// return err
//}

_ = d.Set("type", r.Payload.Type)

values := readClusterPreserveValues(d)
Expand Down
33 changes: 7 additions & 26 deletions metakube/resource_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,6 @@ func TestAccMetaKubeCluster_Openstack_Basic(t *testing.T) {
return nil
}),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.audit_logging", "false"),
// Test credential
testResourceInstanceState("metakube_cluster.acctest_cluster", func(is *terraform.InstanceState) error {
v, ok := is.Attributes["credential"]
if !ok && cluster.Credential != "" {
return fmt.Errorf("cluster credential not set")
}
if want := cluster.Credential; want != v {
return fmt.Errorf("want .Credential=%s, got %s", want, v)
}

return nil
}),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "type", "kubernetes"),
resource.TestCheckResourceAttrSet("metakube_cluster.acctest_cluster", "creation_timestamp"),
resource.TestCheckResourceAttrSet("metakube_cluster.acctest_cluster", "deletion_timestamp"),
Expand Down Expand Up @@ -208,18 +196,6 @@ func TestAccMetaKubeCluster_Openstack_Basic(t *testing.T) {
return nil
}),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "spec.0.audit_logging", "true"),
// Test credential
testResourceInstanceState("metakube_cluster.acctest_cluster", func(is *terraform.InstanceState) error {
v, ok := is.Attributes["credential"]
if !ok && cluster.Credential != "" {
return fmt.Errorf("cluster credential not set")
}
if want := cluster.Credential; want != v {
return fmt.Errorf("want .Credential=%s, got %s", want, v)
}

return nil
}),
resource.TestCheckResourceAttr("metakube_cluster.acctest_cluster", "type", "kubernetes"),
resource.TestCheckResourceAttrSet("metakube_cluster.acctest_cluster", "creation_timestamp"),
resource.TestCheckResourceAttrSet("metakube_cluster.acctest_cluster", "deletion_timestamp"),
Expand All @@ -243,8 +219,13 @@ func TestAccMetaKubeCluster_Openstack_UpgradeVersion(t *testing.T) {
versionK8s17 := os.Getenv(testEnvK8sVersion)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckForOpenstack(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheckForOpenstack(t) },
Providers: testAccProviders,
ExternalProviders: map[string]resource.ExternalProvider{
"openstack": {
Source: "terraform-provider-openstack/openstack",
},
},
CheckDestroy: testAccCheckMetaKubeClusterDestroy,
Steps: []resource.TestStep{
{
Expand Down
15 changes: 9 additions & 6 deletions metakube/resource_node_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package metakube
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"net/http"
"strings"
"time"

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -76,12 +77,14 @@ type nodeSpecPreservedValues struct {
}

func readNodeDeploymentPreservedValues(d *schema.ResourceData) *nodeSpecPreservedValues {
if _, ok := d.GetOk("spec.0.template.0.cloud.0.azure.0"); !ok {
return &nodeSpecPreservedValues{}
}
return &nodeSpecPreservedValues{
azure: expandAzureNodeSpec(d.Get("spec.0.template.0.cloud.0.azure").([]interface{})),
if v, ok := d.GetOk("spec.0.template.0.cloud.0.azure"); ok {
if vv, ok := v.([]interface{}); ok && len(vv) == 1 {
return &nodeSpecPreservedValues{
azure: expandAzureNodeSpec(vv),
}
}
}
return &nodeSpecPreservedValues{}
}

func resourceNodeDeploymentCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
Loading

0 comments on commit f473e25

Please sign in to comment.