Skip to content

Commit 5e5bb4d

Browse files
committed
support for metadata service in instance and template
1 parent b8b34c4 commit 5e5bb4d

15 files changed

+243
-4
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ require (
5858
replace github.com/softlayer/softlayer-go v1.0.3 => github.com/IBM-Cloud/softlayer-go v1.0.5-tf
5959

6060
replace github.com/dgrijalva/jwt-go v3.2.0+incompatible => github.com/golang-jwt/jwt v3.2.1+incompatible
61+
replace github.com/IBM/vpc-go-sdk => ./common/github.com/IBM/vpc-go-sdk

ibm/data_source_ibm_is_instance.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ func dataSourceIBMISInstance() *schema.Resource {
4040
Description: "Instance name",
4141
},
4242

43+
isInstanceMetadataServiceEnabled: {
44+
Type: schema.TypeBool,
45+
Computed: true,
46+
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
47+
},
48+
4349
isInstancePEM: {
4450
Type: schema.TypeString,
4551
Optional: true,
@@ -552,6 +558,9 @@ func instanceGetByName(d *schema.ResourceData, meta interface{}, name string) er
552558
if instance.Profile != nil {
553559
d.Set(isInstanceProfile, *instance.Profile.Name)
554560
}
561+
if instance.MetadataService != nil {
562+
d.Set(isInstanceMetadataServiceEnabled, instance.MetadataService.Enabled)
563+
}
555564
cpuList := make([]map[string]interface{}, 0)
556565
if instance.Vcpu != nil {
557566
currentCPU := map[string]interface{}{}

ibm/data_source_ibm_is_instance_template.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func dataSourceIBMISInstanceTemplate() *schema.Resource {
101101
Computed: true,
102102
Description: "The amount of bandwidth (in megabits per second) allocated exclusively to instance storage volumes",
103103
},
104+
isInstanceTemplateMetadataServiceEnabled: {
105+
Type: schema.TypeBool,
106+
Computed: true,
107+
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
108+
},
104109
isInstanceTemplateVolumeAttachments: {
105110
Type: schema.TypeList,
106111
Computed: true,
@@ -299,6 +304,11 @@ func dataSourceIBMISInstanceTemplateRead(context context.Context, d *schema.Reso
299304
}
300305
d.Set(isInstanceTemplateKeys, keys)
301306
}
307+
308+
if instance.MetadataService != nil {
309+
d.Set(isInstanceTemplateMetadataServiceEnabled, instance.MetadataService.Enabled)
310+
}
311+
302312
if instance.Profile != nil {
303313
instanceProfileIntf := instance.Profile
304314
identity := instanceProfileIntf.(*vpcv1.InstanceProfileIdentity)
@@ -473,6 +483,11 @@ func dataSourceIBMISInstanceTemplateRead(context context.Context, d *schema.Reso
473483
}
474484
d.Set(isInstanceTemplateKeys, keys)
475485
}
486+
487+
if instance.MetadataService != nil {
488+
d.Set(isInstanceTemplateMetadataServiceEnabled, instance.MetadataService.Enabled)
489+
}
490+
476491
if instance.Profile != nil {
477492
instanceProfileIntf := instance.Profile
478493
identity := instanceProfileIntf.(*vpcv1.InstanceProfileIdentity)

ibm/data_source_ibm_is_instance_templates.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ func dataSourceIBMISInstanceTemplates() *schema.Resource {
9999
Type: schema.TypeString,
100100
Computed: true,
101101
},
102+
isInstanceTemplateMetadataServiceEnabled: {
103+
Type: schema.TypeBool,
104+
Computed: true,
105+
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
106+
},
102107
isInstanceTemplatesHref: {
103108
Type: schema.TypeString,
104109
Computed: true,
@@ -328,6 +333,10 @@ func dataSourceIBMISInstanceTemplatesRead(d *schema.ResourceData, meta interface
328333
template["placement_target"] = []map[string]interface{}{placementTargetMap}
329334
}
330335

336+
if instance.MetadataService != nil {
337+
template[isInstanceTemplateMetadataServiceEnabled] = *instance.MetadataService.Enabled
338+
}
339+
331340
if instance.Keys != nil {
332341
keys := []string{}
333342
for _, intfc := range instance.Keys {

ibm/data_source_ibm_is_instances.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ func dataSourceIBMISInstances() *schema.Resource {
114114
Computed: true,
115115
Description: "Instance memory",
116116
},
117+
isInstanceMetadataServiceEnabled: {
118+
Type: schema.TypeBool,
119+
Computed: true,
120+
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
121+
},
117122
"status": {
118123
Type: schema.TypeString,
119124
Computed: true,
@@ -651,6 +656,9 @@ func instancesList(d *schema.ResourceData, meta interface{}) error {
651656
l["crn"] = *instance.CRN
652657
l["name"] = *instance.Name
653658
l["memory"] = *instance.Memory
659+
if instance.MetadataService != nil {
660+
l[isInstanceMetadataServiceEnabled] = *instance.MetadataService.Enabled
661+
}
654662
l["status"] = *instance.Status
655663
l["resource_group"] = *instance.ResourceGroup.ID
656664
l["vpc"] = *instance.VPC.ID

ibm/resource_ibm_is_instance.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ const (
9595
isPlacementTargetDedicatedHostGroup = "dedicated_host_group"
9696
isInstancePlacementTarget = "placement_target"
9797
isPlacementTargetPlacementGroup = "placement_group"
98+
isInstanceMetadataServiceEnabled = "metadata_service_enabled"
9899
)
99100

100101
func resourceIBMISInstance() *schema.Resource {
@@ -565,6 +566,13 @@ func resourceIBMISInstance() *schema.Resource {
565566
},
566567
},
567568
},
569+
isInstanceMetadataServiceEnabled: {
570+
Type: schema.TypeBool,
571+
Optional: true,
572+
Default: true,
573+
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
574+
},
575+
568576
ResourceControllerURL: {
569577
Type: schema.TypeString,
570578
Computed: true,
@@ -1165,6 +1173,10 @@ func instanceCreateByTemplate(d *schema.ResourceData, meta interface{}, profile,
11651173
log.Printf("[INFO] Instance : %s", *instance.ID)
11661174
d.Set(isInstanceStatus, instance.Status)
11671175

1176+
if instance.MetadataService != nil {
1177+
d.Set(isInstanceMetadataServiceEnabled, instance.MetadataService.Enabled)
1178+
}
1179+
11681180
_, err = isWaitForInstanceAvailable(sess, d.Id(), d.Timeout(schema.TimeoutCreate), d)
11691181
if err != nil {
11701182
return err
@@ -1368,6 +1380,10 @@ func instanceCreateByVolume(d *schema.ResourceData, meta interface{}, profile, n
13681380

13691381
}
13701382

1383+
metadataServiceEnabled := d.Get(isInstanceTemplateMetadataServiceEnabled).(bool)
1384+
instanceproto.MetadataService = &vpcv1.InstanceMetadataServicePrototype{
1385+
Enabled: &metadataServiceEnabled,
1386+
}
13711387
options := &vpcv1.CreateInstanceOptions{
13721388
InstancePrototype: instanceproto,
13731389
}
@@ -1730,7 +1746,9 @@ func instanceGet(d *schema.ResourceData, meta interface{}, id string) error {
17301746
d.Set(isInstanceResourceGroup, *instance.ResourceGroup.ID)
17311747
d.Set(ResourceGroupName, *instance.ResourceGroup.Name)
17321748
}
1733-
1749+
if instance.MetadataService != nil {
1750+
d.Set(isInstanceMetadataServiceEnabled, instance.MetadataService.Enabled)
1751+
}
17341752
if instance.Disks != nil {
17351753
disks := []map[string]interface{}{}
17361754
for _, disksItem := range instance.Disks {
@@ -2064,6 +2082,28 @@ func instanceUpdate(d *schema.ResourceData, meta interface{}) error {
20642082
}
20652083
}
20662084

2085+
if d.HasChange(isInstanceMetadataServiceEnabled) && !d.IsNewResource() {
2086+
enabled := d.Get(isInstanceMetadataServiceEnabled).(bool)
2087+
updatedoptions := &vpcv1.UpdateInstanceOptions{
2088+
ID: &id,
2089+
}
2090+
instancePatchModel := &vpcv1.InstancePatch{
2091+
MetadataService: &vpcv1.InstanceMetadataServicePatch{
2092+
Enabled: &enabled,
2093+
},
2094+
}
2095+
instancePatch, err := instancePatchModel.AsPatch()
2096+
if err != nil {
2097+
return fmt.Errorf("Error calling asPatch for InstancePatch: %s", err)
2098+
}
2099+
updatedoptions.InstancePatch = instancePatch
2100+
2101+
_, _, err = instanceC.UpdateInstance(updatedoptions)
2102+
if err != nil {
2103+
return err
2104+
}
2105+
}
2106+
20672107
if d.HasChange(isInstanceProfile) && !d.IsNewResource() {
20682108

20692109
getinsOptions := &vpcv1.GetInstanceOptions{

ibm/resource_ibm_is_instance_template.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
isInstanceTemplateDedicatedHostGroup = "dedicated_host_group"
4646
isInstanceTemplateResourceType = "resource_type"
4747
isInstanceTemplateVolumeDeleteOnInstanceDelete = "delete_volume_on_instance_delete"
48+
isInstanceTemplateMetadataServiceEnabled = "metadata_service_enabled"
4849
)
4950

5051
func resourceIBMISInstanceTemplate() *schema.Resource {
@@ -78,6 +79,14 @@ func resourceIBMISInstanceTemplate() *schema.Resource {
7879
Description: "Instance Template name",
7980
},
8081

82+
isInstanceTemplateMetadataServiceEnabled: {
83+
Type: schema.TypeBool,
84+
Optional: true,
85+
ForceNew: true,
86+
Default: true,
87+
Description: "Indicates whether the metadata service endpoint is available to the virtual server instance",
88+
},
89+
8190
isInstanceTemplateVPC: {
8291
Type: schema.TypeString,
8392
ForceNew: true,
@@ -465,6 +474,10 @@ func instanceTemplateCreate(d *schema.ResourceData, meta interface{}, profile, n
465474
ID: &vpcID,
466475
},
467476
}
477+
metadataServiceEnabled := d.Get(isInstanceTemplateMetadataServiceEnabled).(bool)
478+
instanceproto.MetadataService = &vpcv1.InstanceMetadataServicePrototype{
479+
Enabled: &metadataServiceEnabled,
480+
}
468481

469482
if dHostIdInf, ok := d.GetOk(isPlacementTargetDedicatedHost); ok {
470483
dHostIdStr := dHostIdInf.(string)
@@ -739,6 +752,9 @@ func instanceTemplateGet(d *schema.ResourceData, meta interface{}, ID string) er
739752
if instance.TotalVolumeBandwidth != nil {
740753
d.Set(isInstanceTotalVolumeBandwidth, int(*instance.TotalVolumeBandwidth))
741754
}
755+
if instance.MetadataService != nil {
756+
d.Set(isInstanceTemplateMetadataServiceEnabled, instance.MetadataService.Enabled)
757+
}
742758

743759
var placementTargetMap map[string]interface{}
744760
if instance.PlacementTarget != nil {

ibm/resource_ibm_is_instance_template_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,32 @@ func TestAccIBMISInstanceTemplate_basic(t *testing.T) {
4242
})
4343
}
4444

45+
func TestAccIBMISInstanceTemplate_metadata_service(t *testing.T) {
46+
randInt := acctest.RandIntRange(10, 100)
47+
48+
publicKey := strings.TrimSpace(`
49+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDVtuCfWKVGKaRmaRG6JQZY8YdxnDgGzVOK93IrV9R5Hl0JP1oiLLWlZQS2reAKb8lBqyDVEREpaoRUDjqDqXG8J/kR42FKN51su914pjSBc86wJ02VtT1Wm1zRbSg67kT+g8/T1jCgB5XBODqbcICHVP8Z1lXkgbiHLwlUrbz6OZkGJHo/M/kD1Eme8lctceIYNz/Ilm7ewMXZA4fsidpto9AjyarrJLufrOBl4MRVcZTDSJ7rLP982aHpu9pi5eJAjOZc7Og7n4ns3NFppiCwgVMCVUQbN5GBlWhZ1OsT84ZiTf+Zy8ew+Yg5T7Il8HuC7loWnz+esQPf0s3xhC/kTsGgZreIDoh/rxJfD67wKXetNSh5RH/n5BqjaOuXPFeNXmMhKlhj9nJ8scayx/wsvOGuocEIkbyJSLj3sLUU403OafgatEdnJOwbqg6rUNNF5RIjpJpL7eEWlKIi1j9LyhmPJ+fEO7TmOES82VpCMHpLbe4gf/MhhJ/Xy8DKh9s= root@ffd8363b1226
50+
`)
51+
vpcName := fmt.Sprintf("tf-testvpc%d", randInt)
52+
subnetName := fmt.Sprintf("tf-testsubnet%d", randInt)
53+
templateName := fmt.Sprintf("tf-testtemplate%d", randInt)
54+
sshKeyName := fmt.Sprintf("tf-testsshkey%d", randInt)
55+
resource.Test(t, resource.TestCase{
56+
PreCheck: func() { testAccPreCheck(t) },
57+
Providers: testAccProviders,
58+
CheckDestroy: testAccCheckIBMISInstanceTemplateDestroy,
59+
Steps: []resource.TestStep{
60+
{
61+
Config: testAccCheckIBMISInstanceMetadataServiceTemplateConfig(vpcName, subnetName, sshKeyName, publicKey, templateName, "true"),
62+
Check: resource.ComposeTestCheckFunc(
63+
resource.TestCheckResourceAttr(
64+
"ibm_is_instance_template.instancetemplate1", "metadata_service_enabled", "true"),
65+
),
66+
},
67+
},
68+
})
69+
}
70+
4571
func TestAccIBMISInstanceTemplate_WithVolumeAttachment(t *testing.T) {
4672
randInt := acctest.RandIntRange(10, 100)
4773

@@ -134,6 +160,51 @@ func testAccCheckIBMISInstanceTemplateConfig(vpcName, subnetName, sshKeyName, pu
134160

135161
}
136162

163+
func testAccCheckIBMISInstanceMetadataServiceTemplateConfig(vpcName, subnetName, sshKeyName, publicKey, templateName, metadataService string) string {
164+
return fmt.Sprintf(`
165+
provider "ibm" {
166+
generation = 2
167+
}
168+
169+
resource "ibm_is_vpc" "vpc2" {
170+
name = "%s"
171+
}
172+
173+
resource "ibm_is_subnet" "subnet2" {
174+
name = "%s"
175+
vpc = ibm_is_vpc.vpc2.id
176+
zone = "us-south-2"
177+
ipv4_cidr_block = "10.240.64.0/28"
178+
}
179+
180+
resource "ibm_is_ssh_key" "sshkey" {
181+
name = "%s"
182+
public_key = "%s"
183+
}
184+
185+
data "ibm_is_images" "is_images" {
186+
}
187+
188+
resource "ibm_is_instance_template" "instancetemplate1" {
189+
name = "%s"
190+
image = data.ibm_is_images.is_images.images.0.id
191+
profile = "bx2-8x32"
192+
193+
primary_network_interface {
194+
subnet = ibm_is_subnet.subnet2.id
195+
}
196+
197+
vpc = ibm_is_vpc.vpc2.id
198+
zone = "us-south-2"
199+
keys = [ibm_is_ssh_key.sshkey.id]
200+
metadata_service_enabled = %s
201+
}
202+
203+
204+
`, vpcName, subnetName, sshKeyName, publicKey, templateName, metadataService)
205+
206+
}
207+
137208
func testAccCheckIBMISInstanceTemplateWithVolume(vpcName, subnetName, sshKeyName, publicKey, templateName, volAttachName string) string {
138209
return fmt.Sprintf(`
139210
provider "ibm" {

ibm/resource_ibm_is_instance_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVE
182182
})
183183
}
184184

185+
func TestAccIBMISInstance_metadata_service(t *testing.T) {
186+
var instance string
187+
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
188+
name := fmt.Sprintf("tf-instnace-%d", acctest.RandIntRange(10, 100))
189+
subnetname := fmt.Sprintf("tf-subnet-%d", acctest.RandIntRange(10, 100))
190+
publicKey := strings.TrimSpace(`
191+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCKVmnMOlHKcZK8tpt3MP1lqOLAcqcJzhsvJcjscgVERRN7/9484SOBJ3HSKxxNG5JN8owAjy5f9yYwcUg+JaUVuytn5Pv3aeYROHGGg+5G346xaq3DAwX6Y5ykr2fvjObgncQBnuU5KHWCECO/4h8uWuwh/kfniXPVjFToc+gnkqA+3RKpAecZhFXwfalQ9mMuYGFxn+fwn8cYEApsJbsEmb0iJwPiZ5hjFC8wREuiTlhPHDgkBLOiycd20op2nXzDbHfCHInquEe/gYxEitALONxm0swBOwJZwlTDOB7C6y2dzlrtxr1L59m7pCkWI4EtTRLvleehBoj3u7jB4usR
192+
`)
193+
sshname := fmt.Sprintf("tf-ssh-%d", acctest.RandIntRange(10, 100))
194+
195+
resource.Test(t, resource.TestCase{
196+
PreCheck: func() { testAccPreCheck(t) },
197+
Providers: testAccProviders,
198+
CheckDestroy: testAccCheckIBMISInstanceDestroy,
199+
Steps: []resource.TestStep{
200+
{
201+
Config: testAccCheckIBMISInstanceWithMetaConfig(vpcname, subnetname, sshname, publicKey, name, true),
202+
Check: resource.ComposeTestCheckFunc(
203+
testAccCheckIBMISInstanceExists("ibm_is_instance.testacc_instance", instance),
204+
resource.TestCheckResourceAttr(
205+
"ibm_is_instance.testacc_instance", "metadata_service_enabled", "true"),
206+
),
207+
},
208+
},
209+
})
210+
}
185211
func TestAccIBMISInstance_profile(t *testing.T) {
186212
var instance string
187213
vpcname := fmt.Sprintf("tf-vpc-%d", acctest.RandIntRange(10, 100))
@@ -538,6 +564,42 @@ func testAccCheckIBMISInstanceExists(n string, instance string) resource.TestChe
538564
}
539565
}
540566

567+
func testAccCheckIBMISInstanceWithMetaConfig(vpcname, subnetname, sshname, publicKey, name string, metadata_service_enabled bool) string {
568+
return fmt.Sprintf(`
569+
resource "ibm_is_vpc" "testacc_vpc" {
570+
name = "%s"
571+
}
572+
573+
resource "ibm_is_subnet" "testacc_subnet" {
574+
name = "%s"
575+
vpc = ibm_is_vpc.testacc_vpc.id
576+
zone = "%s"
577+
ipv4_cidr_block = "%s"
578+
}
579+
580+
resource "ibm_is_ssh_key" "testacc_sshkey" {
581+
name = "%s"
582+
public_key = "%s"
583+
}
584+
585+
resource "ibm_is_instance" "testacc_instance" {
586+
name = "%s"
587+
image = "%s"
588+
profile = "%s"
589+
primary_network_interface {
590+
subnet = ibm_is_subnet.testacc_subnet.id
591+
}
592+
vpc = ibm_is_vpc.testacc_vpc.id
593+
zone = "%s"
594+
keys = [ibm_is_ssh_key.testacc_sshkey.id]
595+
network_interfaces {
596+
subnet = ibm_is_subnet.testacc_subnet.id
597+
name = "eth1"
598+
}
599+
metadata_service_enabled=%v
600+
}`, vpcname, subnetname, ISZoneName, ISCIDR, sshname, publicKey, name, isImage, instanceProfileName, ISZoneName, metadata_service_enabled)
601+
}
602+
541603
func testAccCheckIBMISInstanceConfig(vpcname, subnetname, sshname, publicKey, name string) string {
542604
return fmt.Sprintf(`
543605
resource "ibm_is_vpc" "testacc_vpc" {

0 commit comments

Comments
 (0)