Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic_config to node template resource #15

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions rancher2/resource_rancher2_cloud_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,35 @@ func resourceRancher2CloudCredential() *schema.Resource {
}

func resourceRancher2CloudCredentialCreate(d *schema.ResourceData, meta interface{}) error {
cloudCredential := expandCloudCredential(d)

log.Printf("[INFO] Creating Cloud Credential %s", cloudCredential.Name)

client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}

nodeDriver := d.Get("driver").(string)
err = meta.(*Config).activateNodeDriver(nodeDriver)
cloudCredential, err := expandCloudCredential(d, client.NodeDriver)

log.Printf("[INFO] Creating Cloud Credential %s", cloudCredential.Name)

driverID := d.Get("driver").(string)
if cloudCredential.genericCredentialConfig != nil {
driverID = cloudCredential.genericCredentialConfig.driverID
}
err = meta.(*Config).activateNodeDriver(driverID)
if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{},
Target: []string{"active"},
Refresh: nodeDriverStateRefreshFunc(client, nodeDriver),
Refresh: nodeDriverStateRefreshFunc(client, driverID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}
_, waitErr := stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf("[ERROR] waiting for cloud credential (%s) to be activated: %s", nodeDriver, waitErr)
return fmt.Errorf("[ERROR] waiting for cloud credential (%s) to be activated: %s", driverID, waitErr)
}

newCloudCredential := &CloudCredential{}
Expand Down Expand Up @@ -138,7 +141,11 @@ func resourceRancher2CloudCredentialUpdate(d *schema.ResourceData, meta interfac
case vmwarevsphereConfigDriver:
update["vmwarevspherecredentialConfig"] = expandCloudCredentialVsphere(d.Get("vsphere_credential_config").([]interface{}))
default:
return fmt.Errorf("[ERROR] updating cloud credential: Unsupported driver \"%s\"", driver)
v, err := expandCloudCredentialGeneric(d.Get("generic_credential_config").([]interface{}), client.NodeDriver)
if err != nil {
return err
}
update[v.driverName+credentialConfigKeySuffix] = v
}

newCloudCredential := &CloudCredential{}
Expand Down
106 changes: 106 additions & 0 deletions rancher2/resource_rancher2_cloud_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,47 @@ resource "rancher2_cloud_credential" "foo" {
digitalocean_credential_config {
access_token = "XXXXXXXXXXXXXXXXXXXX"
}
}
`
testAccRancher2CloudCredentialConfigGeneric = `
resource "rancher2_cloud_credential" "foo" {
name = "foo"
description= "Terraform cloudCredential acceptance test"
generic_credential_config {
driver = "rackspace"
config {
username = "XXXXXXXXXXXXXXXXXXXX"
apiKey = "XXXXXXXXXXXXXXXXXXXX"
}
}
}
`

testAccRancher2CloudCredentialUpdateConfigGeneric = `
resource "rancher2_cloud_credential" "foo" {
name = "foo"
description= "Terraform cloudCredential acceptance test - updated"
generic_credential_config {
driver = "rackspace"
config {
username = "YYYYYYYYYYYYYYYYYYYY"
apiKey = "YYYYYYYYYYYYYYYYYYYY"
}
}
}
`

testAccRancher2CloudCredentialRecreateConfigGeneric = `
resource "rancher2_cloud_credential" "foo" {
name = "foo"
description= "Terraform cloudCredential acceptance test"
generic_credential_config {
driver = "rackspace"
config {
username = "XXXXXXXXXXXXXXXXXXXX"
apiKey = "XXXXXXXXXXXXXXXXXXXX"
}
}
}
`
testAccRancher2CloudCredentialConfigOpenstack = `
Expand Down Expand Up @@ -361,6 +402,71 @@ func TestAccRancher2CloudCredential_disappears_Digitalocean(t *testing.T) {
})
}

func TestAccRancher2CloudCredential_basic_Generic(t *testing.T) {
var cloudCredential *CloudCredential

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancher2CloudCredentialDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancher2CloudCredentialConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CloudCredentialExists(testAccRancher2CloudCredentialType+".foo", cloudCredential),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "description", "Terraform cloudCredential acceptance test"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "driver", "rackspace"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "generic_credential_config.0.config.username", "XXXXXXXXXXXXXXXXXXXX"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "generic_credential_config.0.config.apiKey", "XXXXXXXXXXXXXXXXXXXX"),
),
},
resource.TestStep{
Config: testAccRancher2CloudCredentialUpdateConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CloudCredentialExists(testAccRancher2CloudCredentialType+".foo", cloudCredential),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "description", "Terraform cloudCredential acceptance test - updated"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "driver", "rackspace"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "generic_credential_config.0.config.username", "YYYYYYYYYYYYYYYYYYYY"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "generic_credential_config.0.config.apiKey", "YYYYYYYYYYYYYYYYYYYY"),
),
},
resource.TestStep{
Config: testAccRancher2CloudCredentialRecreateConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CloudCredentialExists(testAccRancher2CloudCredentialType+".foo", cloudCredential),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "name", "foo"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "description", "Terraform cloudCredential acceptance test"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "driver", "rackspace"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "generic_credential_config.0.config.username", "XXXXXXXXXXXXXXXXXXXX"),
resource.TestCheckResourceAttr(testAccRancher2CloudCredentialType+".foo", "generic_credential_config.0.config.apiKey", "XXXXXXXXXXXXXXXXXXXX"),
),
},
},
})
}

func TestAccRancher2CloudCredential_disappears_Generic(t *testing.T) {
var cloudCredential *CloudCredential

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancher2CloudCredentialDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancher2CloudCredentialConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2CloudCredentialExists(testAccRancher2CloudCredentialType+".foo", cloudCredential),
testAccRancher2CloudCredentialDisappears(cloudCredential),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccRancher2CloudCredential_basic_Openstack(t *testing.T) {
var cloudCredential *CloudCredential

Expand Down
27 changes: 20 additions & 7 deletions rancher2/resource_rancher2_node_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,37 @@ func resourceRancher2NodeTemplate() *schema.Resource {
}

func resourceRancher2NodeTemplateCreate(d *schema.ResourceData, meta interface{}) error {
nodeTemplate := expandNodeTemplate(d)

log.Printf("[INFO] Creating Node Template %s", nodeTemplate.Name)

client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}
nodeTemplate, err := expandNodeTemplate(d, client.NodeDriver)
if err != nil {
return err
}

err = meta.(*Config).activateNodeDriver(nodeTemplate.Driver)
log.Printf("[INFO] Creating Node Template %s", nodeTemplate.Name)

driverID := nodeTemplate.Driver
if nodeTemplate.genericConfig != nil {
driverID = nodeTemplate.genericConfig.driverID
}
err = meta.(*Config).activateNodeDriver(driverID)
if err != nil {
return err
}

stateConf := &resource.StateChangeConf{
Pending: []string{},
Target: []string{"active"},
Refresh: nodeDriverStateRefreshFunc(client, nodeTemplate.Driver),
Refresh: nodeDriverStateRefreshFunc(client, driverID),
Timeout: d.Timeout(schema.TimeoutCreate),
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}
_, waitErr := stateConf.WaitForState()
if waitErr != nil {
return fmt.Errorf("[ERROR] waiting for node driver (%s) to be activated: %s", nodeTemplate.Driver, waitErr)
return fmt.Errorf("[ERROR] waiting for node driver (%s) to be activated: %s", driverID, waitErr)
}

newNodeTemplate := &NodeTemplate{}
Expand Down Expand Up @@ -151,6 +157,13 @@ func resourceRancher2NodeTemplateUpdate(d *schema.ResourceData, meta interface{}
update["openstackConfig"] = expandOpenstackConfig(d.Get("openstack_config").([]interface{}))
case vmwarevsphereConfigDriver:
update["vmwarevsphereConfig"] = expandVsphereConfig(d.Get("vsphere_config").([]interface{}))
default:
configKey := fmt.Sprintf("%sConfig", driver)
genericConfig, err := expandGenericNodeTemplateConfig(d.Get("generic_config").([]interface{}), client.NodeDriver)
if err != nil {
return err
}
update[configKey] = genericConfig.config
}

newNodeTemplate := &NodeTemplate{}
Expand Down
109 changes: 109 additions & 0 deletions rancher2/resource_rancher2_node_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,49 @@ resource "rancher2_node_template" "foo" {
region = "region-XXXXXXXX"
}
}
`
testAccRancher2NodeTemplateConfigGeneric = testAccRancher2CloudCredentialConfigGeneric + `
resource "rancher2_node_template" "foo" {
name = "foo"
description = "Terraform node driver generic acceptance test"
cloud_credential_id = "${rancher2_cloud_credential.foo.id}"
generic_config {
driver = "rackspace"
config {
flavorId = "flavor-XXXXXXXX"
region = "region-XXXXXXXX"
}
}
}
`

testAccRancher2NodeTemplateUpdateConfigGeneric = testAccRancher2CloudCredentialConfigGeneric + `
resource "rancher2_node_template" "foo" {
name = "foo2"
description = "Terraform node driver generic acceptance test - updated"
cloud_credential_id = "${rancher2_cloud_credential.foo.id}"
generic_config {
driver = "rackspace"
config {
flavorId = "flavor-YYYYYYYY"
region = "region-YYYYYYYY"
}
}
}
`
testAccRancher2NodeTemplateRecreateConfigGeneric = testAccRancher2CloudCredentialConfigGeneric + `
resource "rancher2_node_template" "foo" {
name = "foo"
description = "Terraform node driver generic acceptance test"
cloud_credential_id = "${rancher2_cloud_credential.foo.id}"
generic_config {
driver = "rackspace"
config {
flavorId = "flavor-XXXXXXXX"
region = "region-XXXXXXXX"
}
}
}
`
testAccRancher2NodeTemplateConfigOpenstack = testAccRancher2CloudCredentialConfigOpenstack + `
resource "rancher2_node_template" "foo" {
Expand Down Expand Up @@ -405,6 +448,72 @@ func TestAccRancher2NodeTemplate_disappears_Digitalocean(t *testing.T) {
})
}

func TestAccRancher2NodeTemplate_basic_Generic(t *testing.T) {
var nodeTemplate *NodeTemplate

name := testAccRancher2NodeTemplateType + ".foo"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancher2NodeTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancher2NodeTemplateConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2NodeTemplateExists(name, nodeTemplate),
resource.TestCheckResourceAttr(name, "name", "foo"),
resource.TestCheckResourceAttr(name, "description", "Terraform node driver generic acceptance test"),
resource.TestCheckResourceAttr(name, "driver", "rackspace"),
resource.TestCheckResourceAttr(name, "generic_config.0.config.flavorId", "flavor-XXXXXXXX"),
resource.TestCheckResourceAttr(name, "generic_config.0.config.region", "region-XXXXXXXX"),
),
},
resource.TestStep{
Config: testAccRancher2NodeTemplateUpdateConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2NodeTemplateExists(name, nodeTemplate),
resource.TestCheckResourceAttr(name, "name", "foo2"),
resource.TestCheckResourceAttr(name, "description", "Terraform node driver generic acceptance test - updated"),
resource.TestCheckResourceAttr(name, "driver", "rackspace"),
resource.TestCheckResourceAttr(name, "generic_config.0.config.flavorId", "flavor-YYYYYYYY"),
resource.TestCheckResourceAttr(name, "generic_config.0.config.region", "region-YYYYYYYY"),
),
},
resource.TestStep{
Config: testAccRancher2NodeTemplateRecreateConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2NodeTemplateExists(name, nodeTemplate),
resource.TestCheckResourceAttr(name, "name", "foo"),
resource.TestCheckResourceAttr(name, "description", "Terraform node driver generic acceptance test"),
resource.TestCheckResourceAttr(name, "driver", "rackspace"),
resource.TestCheckResourceAttr(name, "generic_config.0.config.flavorId", "flavor-XXXXXXXX"),
resource.TestCheckResourceAttr(name, "generic_config.0.config.region", "region-XXXXXXXX"),
),
},
},
})
}

func TestAccRancher2NodeTemplate_disappears_Generic(t *testing.T) {
var nodeTemplate *NodeTemplate

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckRancher2NodeTemplateDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccRancher2NodeTemplateConfigGeneric,
Check: resource.ComposeTestCheckFunc(
testAccCheckRancher2NodeTemplateExists(testAccRancher2NodeTemplateType+".foo", nodeTemplate),
testAccRancher2NodeTemplateDisappears(nodeTemplate),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func TestAccRancher2NodeTemplate_basic_Openstack(t *testing.T) {
var nodeTemplate *NodeTemplate

Expand Down
Loading