Skip to content

Commit

Permalink
added a note for vol attachment and added test case for ins template
Browse files Browse the repository at this point in the history
  • Loading branch information
deepaksibm authored and hkantare committed Apr 15, 2021
1 parent ce8ae7f commit 1c62a9d
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
106 changes: 106 additions & 0 deletions ibm/resource_ibm_is_instance_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package ibm

import (
"fmt"
"strings"
"testing"

"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccIBMISInstanceTemplate_basic(t *testing.T) {
randInt := acctest.RandIntRange(10, 100)

publicKey := strings.TrimSpace(`
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDVtuCfWKVGKaRmaRG6JQZY8YdxnDgGzVOK93IrV9R5Hl0JP1oiLLWlZQS2reAKb8lBqyDVEREpaoRUDjqDqXG8J/kR42FKN51su914pjSBc86wJ02VtT1Wm1zRbSg67kT+g8/T1jCgB5XBODqbcICHVP8Z1lXkgbiHLwlUrbz6OZkGJHo/M/kD1Eme8lctceIYNz/Ilm7ewMXZA4fsidpto9AjyarrJLufrOBl4MRVcZTDSJ7rLP982aHpu9pi5eJAjOZc7Og7n4ns3NFppiCwgVMCVUQbN5GBlWhZ1OsT84ZiTf+Zy8ew+Yg5T7Il8HuC7loWnz+esQPf0s3xhC/kTsGgZreIDoh/rxJfD67wKXetNSh5RH/n5BqjaOuXPFeNXmMhKlhj9nJ8scayx/wsvOGuocEIkbyJSLj3sLUU403OafgatEdnJOwbqg6rUNNF5RIjpJpL7eEWlKIi1j9LyhmPJ+fEO7TmOES82VpCMHpLbe4gf/MhhJ/Xy8DKh9s= root@ffd8363b1226
`)
vpcName := fmt.Sprintf("tf-testvpc%d", randInt)
subnetName := fmt.Sprintf("tf-testsubnet%d", randInt)
templateName := fmt.Sprintf("tf-testtemplate%d", randInt)
sshKeyName := fmt.Sprintf("tf-testsshkey%d", randInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckIBMISInstanceTemplateDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISInstanceTemplateConfig(vpcName, subnetName, sshKeyName, publicKey, templateName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ibm_is_instance_template.instancetemplate1", "name", templateName),
resource.TestCheckResourceAttrSet(
"ibm_is_instance_template.instancetemplate1", "profile"),
),
},
},
})
}

func testAccCheckIBMISInstanceTemplateDestroy(s *terraform.State) error {
sess, _ := testAccProvider.Meta().(ClientSession).VpcV1API()
for _, rs := range s.RootModule().Resources {
if rs.Type != "ibm_is_instance_template" {
continue
}

getInstanceTemplateOptions := vpcv1.GetInstanceTemplateOptions{
ID: &rs.Primary.ID,
}
_, _, err := sess.GetInstanceTemplate(&getInstanceTemplateOptions)

if err == nil {
return fmt.Errorf("instance template still exists: %s", rs.Primary.ID)
}
}
return nil
}

func testAccCheckIBMISInstanceTemplateConfig(vpcName, subnetName, sshKeyName, publicKey, templateName string) string {
return fmt.Sprintf(`
provider "ibm" {
generation = 2
}
resource "ibm_is_vpc" "vpc2" {
name = "%s"
}
resource "ibm_is_subnet" "subnet2" {
name = "%s"
vpc = ibm_is_vpc.vpc2.id
zone = "us-south-2"
ipv4_cidr_block = "10.240.64.0/28"
}
resource "ibm_is_ssh_key" "sshkey" {
name = "%s"
public_key = "%s"
}
data "ibm_is_images" "is_images" {
}
resource "ibm_is_instance_template" "instancetemplate1" {
name = "%s"
image = data.ibm_is_images.is_images.images.0.id
profile = "bx2-8x32"
primary_network_interface {
subnet = ibm_is_subnet.subnet2.id
}
vpc = ibm_is_vpc.vpc2.id
zone = "us-south-2"
keys = [ibm_is_ssh_key.sshkey.id]
}
`, vpcName, subnetName, sshKeyName, publicKey, templateName)

}
2 changes: 2 additions & 0 deletions website/docs/r/is_instance_template.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ The following arguments are supported:
* `delete_volume_on_instance_delete` - (Required, bool) Configured to delete the storage volume to be deleted upon instance deletion.
* `user_data` - (Optional, string) User data provided for the instance.

**NOTE**: `volume_attachments` Volume attachments are not supported for instance groups, so if you plan to use this instance template with an instance group do not include volume attachment for Data volumes. Otherwise, you can add one or more secondary data volumes to be included in the template to be used when you provision the instances with the Template

## Attribute Reference

In addition to all arguments above, the following attributes are exported:
Expand Down

0 comments on commit 1c62a9d

Please sign in to comment.