Skip to content

Commit 71c0ad6

Browse files
authored
feat(legacy-vendor-images):added support for vpc-services (IBM-Cloud#6357)
* feat(legacy-vendor-images):added support for vpc-services * feat(legacy-vendor-images):added support for vpc-services and fixed error handling * cidr error fixed * changed instance profiles * updated sdk version to 0.70.1
1 parent 896c4d1 commit 71c0ad6

11 files changed

+245
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.11
3939
github.com/IBM/vmware-go-sdk v0.1.3
4040
github.com/IBM/vpc-beta-go-sdk v0.8.0
41-
github.com/IBM/vpc-go-sdk v0.67.1
41+
github.com/IBM/vpc-go-sdk v0.70.1
4242
github.com/ScaleFT/sshkeys v0.0.0-20200327173127-6142f742bca5
4343
github.com/akamai/AkamaiOPEN-edgegrid-golang v1.2.2
4444
github.com/akamai/AkamaiOPEN-edgegrid-golang/v5 v5.0.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ github.com/IBM/vmware-go-sdk v0.1.3 h1:uJL3kwzM0jAKsT6Gj9tE5xT9SZBVXVaJvZdxrSMx8
167167
github.com/IBM/vmware-go-sdk v0.1.3/go.mod h1:OyQKRInGGsBaOyE5LIZCqH7b1DZ01BvIYa8BgGy+wWo=
168168
github.com/IBM/vpc-beta-go-sdk v0.8.0 h1:cEPpv4iw3Ba5W2d0AWg3TIbKeJ8y1nPuUuibR5Jt9eE=
169169
github.com/IBM/vpc-beta-go-sdk v0.8.0/go.mod h1:hORgIyTFRzXrZIK9IohaWmCRBBlYiDRagsufi7M6akE=
170-
github.com/IBM/vpc-go-sdk v0.67.1 h1:z0q1af1iItV4kHgreM21vzZtw6XQ13og2GBkX7WCJ8c=
171-
github.com/IBM/vpc-go-sdk v0.67.1/go.mod h1:VL7sy61ybg6tvA60SepoQx7TFe20m7JyNUt+se2tHP4=
170+
github.com/IBM/vpc-go-sdk v0.70.1 h1:6NsbRkiA5gDNxe7cjNx8Pi1j9s0PlhwNQj29wsKZxAo=
171+
github.com/IBM/vpc-go-sdk v0.70.1/go.mod h1:K3vVlje72PYE3ZRt1iouE+jSIq+vCyYzT1HiFC06hUA=
172172
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56 h1:vuquMR410psHNax14XKNWa0Ae/kYgWJcXi0IFuX60N0=
173173
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56/go.mod h1:Zb3OT4l0mf7P/GOs2w2Ilj5sdm5Whoq3pa24dAEBHFc=
174174
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=

ibm/service/vpc/data_source_ibm_is_image.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,34 @@ func DataSourceIBMISImage() *schema.Resource {
197197
Computed: true,
198198
Description: "The type of encryption used on the image",
199199
},
200+
"remote": {
201+
Type: schema.TypeList,
202+
Computed: true,
203+
Description: "If present, this property indicates that the resource associated with this reference is remote and therefore may not be directly retrievable.",
204+
Elem: &schema.Resource{
205+
Schema: map[string]*schema.Schema{
206+
"account": {
207+
Type: schema.TypeList,
208+
Computed: true,
209+
Description: "If present, this property indicates that the referenced resource is remote to this account, and identifies the owning account.",
210+
Elem: &schema.Resource{
211+
Schema: map[string]*schema.Schema{
212+
"id": {
213+
Type: schema.TypeString,
214+
Computed: true,
215+
Description: "The unique identifier for this resource group.",
216+
},
217+
"resource_type": {
218+
Type: schema.TypeString,
219+
Computed: true,
220+
Description: "The resource type.",
221+
},
222+
},
223+
},
224+
},
225+
},
226+
},
227+
},
200228
"source_volume": {
201229
Type: schema.TypeString,
202230
Computed: true,
@@ -366,6 +394,22 @@ func imageGetByName(context context.Context, d *schema.ResourceData, meta interf
366394
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting resource_group: %s", err), "(Data) ibm_is_image", "read", "set-resource_group").GetDiag()
367395
}
368396
}
397+
if image.Remote != nil {
398+
imageRemoteList := []map[string]interface{}{}
399+
imageRemoteMap, err := dataSourceImageRemote(image)
400+
if err != nil {
401+
if err != nil {
402+
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_image", "read", "initialize-client")
403+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
404+
return tfErr.GetDiag()
405+
}
406+
}
407+
imageRemoteList = append(imageRemoteList, imageRemoteMap)
408+
if err = d.Set(isImageRemote, imageRemoteList); err != nil {
409+
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting remote: %s", err), "(Data) ibm_is_image", "read", "set-remote").GetDiag()
410+
}
411+
}
412+
369413
if err = d.Set("os", image.OperatingSystem.Name); err != nil {
370414
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting os: %s", err), "(Data) ibm_is_image", "read", "set-os").GetDiag()
371415
}
@@ -443,6 +487,21 @@ func imageGetById(context context.Context, d *schema.ResourceData, meta interfac
443487
if *image.Status == "deprecated" {
444488
fmt.Printf("[WARN] Given image %s is deprecated and soon will be obsolete.", name)
445489
}
490+
491+
if image.Remote != nil {
492+
imageRemoteList := []map[string]interface{}{}
493+
imageRemoteMap, err := dataSourceImageRemote(*image)
494+
if err != nil {
495+
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_legacy_vendor_images", "read", "initialize-client")
496+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
497+
return tfErr.GetDiag()
498+
}
499+
imageRemoteList = append(imageRemoteList, imageRemoteMap)
500+
if err = d.Set(isImageRemote, imageRemoteList); err != nil {
501+
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting remote: %s", err), "(Data) ibm_is_image", "read", "set-remote").GetDiag()
502+
}
503+
}
504+
446505
if len(image.StatusReasons) > 0 {
447506
if err = d.Set("status_reasons", dataSourceIBMIsImageFlattenStatusReasons(image.StatusReasons)); err != nil {
448507
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting status_reasons: %s", err), "(Data) ibm_is_image", "read", "set-status_reasons").GetDiag()
@@ -571,6 +630,27 @@ func dataSourceImageCollectionCatalogOfferingToMap(imageCatalogOfferingItem vpcv
571630
return imageCatalogOfferingMap
572631
}
573632

633+
func dataSourceImageRemote(imageRemote vpcv1.Image) (map[string]interface{}, error) {
634+
if imageRemote.Remote == nil || imageRemote.Remote.Account == nil {
635+
return nil, nil
636+
}
637+
638+
accountMap := map[string]interface{}{}
639+
640+
if imageRemote.Remote.Account.ID != nil {
641+
accountMap["id"] = *imageRemote.Remote.Account.ID
642+
}
643+
if imageRemote.Remote.Account.ResourceType != nil {
644+
accountMap["resource_type"] = *imageRemote.Remote.Account.ResourceType
645+
}
646+
647+
remoteMap := map[string]interface{}{
648+
"account": []interface{}{accountMap},
649+
}
650+
651+
return remoteMap, nil
652+
}
653+
574654
func dataSourceIBMIsImageFlattenStatusReasons(result []vpcv1.ImageStatusReason) (statusReasons []map[string]interface{}) {
575655
for _, statusReasonsItem := range result {
576656
statusReasons = append(statusReasons, dataSourceIBMIsImageStatusReasonToMap(&statusReasonsItem))

ibm/service/vpc/data_source_ibm_is_image_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ func TestAccIBMISImageDataSource_With_VisibiltyPrivate(t *testing.T) {
174174
})
175175
}
176176

177+
func TestAccIBMISImageDataSourceRemoteAccountId(t *testing.T) {
178+
resName := "data.ibm_is_image.example"
179+
180+
resource.Test(t, resource.TestCase{
181+
PreCheck: func() { acc.TestAccPreCheck(t) },
182+
Providers: acc.TestAccProviders,
183+
Steps: []resource.TestStep{
184+
{
185+
Config: testAccCheckIBMISImageDataSourceWithRemoteAccountId(),
186+
Check: resource.ComposeTestCheckFunc(
187+
// resource.TestCheckResourceAttrSet(resName, "image.0.remote.#"),
188+
// resource.TestCheckResourceAttrSet(resName, "image.0.remote.0.account.#"),
189+
resource.TestCheckResourceAttrSet(resName, "remote.0.account.0.id"),
190+
resource.TestCheckResourceAttrSet(resName, "remote.0.account.0.resource_type"),
191+
),
192+
},
193+
},
194+
})
195+
}
196+
177197
func testAccCheckIBMISImageDataSourceConfig(imageName string) string {
178198
return fmt.Sprintf(`
179199
resource "ibm_is_image" "isExampleImage" {
@@ -248,3 +268,17 @@ func testAccCheckIBMISImageDataSourceWithVisibilityPrivate(imageName, visibility
248268
visibility = "%s"
249269
}`, acc.Image_cos_url, imageName, acc.Image_operating_system, visibility)
250270
}
271+
272+
func testAccCheckIBMISCatalogImageDataSourceRemoteAccountId() string {
273+
return fmt.Sprintf(`
274+
data "ibm_is_images" "test1" {
275+
catalog_managed = true
276+
}`)
277+
}
278+
279+
func testAccCheckIBMISImageDataSourceWithRemoteAccountId() string {
280+
return fmt.Sprintf(`
281+
data "ibm_is_image" "example" {
282+
name = "ibm-ubuntu-18-04-1-minimal-amd64-1"
283+
}`)
284+
}

ibm/service/vpc/data_source_ibm_is_images.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
isImages = "images"
2121
isImagesResourceGroupID = "resource_group"
2222
isImageCatalogManaged = "catalog_managed"
23+
isImageRemoteAccountId = "remote_account_id"
2324
)
2425

2526
func DataSourceIBMISImages() *schema.Resource {
@@ -54,6 +55,11 @@ func DataSourceIBMISImages() *schema.Resource {
5455
Optional: true,
5556
Description: "Whether the image is publicly visible or private to the account",
5657
},
58+
isImageRemoteAccountId: {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
Description: "Filters the collection to images with a remote.account.id property matching the specified account identifier.",
62+
},
5763
isImageUserDataFormat: {
5864
Type: schema.TypeSet,
5965
Elem: &schema.Schema{Type: schema.TypeString},
@@ -180,6 +186,34 @@ func DataSourceIBMISImages() *schema.Resource {
180186
Computed: true,
181187
Description: "The operating system architecture",
182188
},
189+
"remote": {
190+
Type: schema.TypeList,
191+
Computed: true,
192+
Description: "If present, this property indicates that the resource associated with this reference is remote and therefore may not be directly retrievable.",
193+
Elem: &schema.Resource{
194+
Schema: map[string]*schema.Schema{
195+
"account": {
196+
Type: schema.TypeList,
197+
Computed: true,
198+
Description: "If present, this property indicates that the referenced resource is remote to this account, and identifies the owning account.",
199+
Elem: &schema.Resource{
200+
Schema: map[string]*schema.Schema{
201+
"id": {
202+
Type: schema.TypeString,
203+
Computed: true,
204+
Description: "The unique identifier for this resource group.",
205+
},
206+
"resource_type": {
207+
Type: schema.TypeString,
208+
Computed: true,
209+
Description: "The resource type.",
210+
},
211+
},
212+
},
213+
},
214+
},
215+
},
216+
},
183217
"resource_group": {
184218
Type: schema.TypeList,
185219
Computed: true,
@@ -338,6 +372,10 @@ func imageList(context context.Context, d *schema.ResourceData, meta interface{}
338372
visibility = v.(string)
339373
}
340374

375+
var remoteAccountId string
376+
if v, ok := d.GetOk(isImageRemoteAccountId); ok {
377+
remoteAccountId = v.(string)
378+
}
341379
var status string
342380
if v, ok := d.GetOk(isImageStatus); ok {
343381
status = v.(string)
@@ -354,6 +392,19 @@ func imageList(context context.Context, d *schema.ResourceData, meta interface{}
354392
if imageName != "" {
355393
listImagesOptions.SetName(imageName)
356394
}
395+
396+
if remoteAccountId != "" {
397+
if remoteAccountId == "user" {
398+
remoteAccountId = "null"
399+
listImagesOptions.SetRemoteAccountID(remoteAccountId)
400+
} else if remoteAccountId == "provider" {
401+
remoteAccountId = "not:null"
402+
listImagesOptions.SetRemoteAccountID(remoteAccountId)
403+
} else {
404+
listImagesOptions.SetRemoteAccountID(remoteAccountId)
405+
}
406+
}
407+
357408
if visibility != "" {
358409
listImagesOptions.SetVisibility(visibility)
359410
}
@@ -454,6 +505,21 @@ func imageList(context context.Context, d *schema.ResourceData, meta interface{}
454505
catalogOfferingList = append(catalogOfferingList, catalogOfferingMap)
455506
l[isImageCatalogOffering] = catalogOfferingList
456507
}
508+
509+
if image.Remote != nil {
510+
imageRemoteMap, err := dataSourceImageRemote(image)
511+
if err != nil {
512+
if err != nil {
513+
tfErr := flex.DiscriminatedTerraformErrorf(err, err.Error(), "(Data) ibm_is_image", "read", "initialize-client")
514+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
515+
return tfErr.GetDiag()
516+
}
517+
}
518+
if len(imageRemoteMap) > 0 {
519+
l["remote"] = []interface{}{imageRemoteMap}
520+
}
521+
}
522+
457523
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *image.CRN, "", isImageAccessTagType)
458524
if err != nil {
459525
log.Printf(

ibm/service/vpc/data_source_ibm_is_images_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,25 @@ func TestAccIBMISImageDataSource_With_FilterVisibilty(t *testing.T) {
9696
})
9797
}
9898

99+
func TestAccIBMISImageDataSource_With_FilterRemoteAccountId(t *testing.T) {
100+
resName := "data.ibm_is_images.example"
101+
resource.Test(t, resource.TestCase{
102+
PreCheck: func() { acc.TestAccPreCheck(t) },
103+
Providers: acc.TestAccProviders,
104+
Steps: []resource.TestStep{
105+
{
106+
Config: testAccCheckIBMISImagesDataSourceWithRemoteAccountId("provider"),
107+
Check: resource.ComposeTestCheckFunc(
108+
resource.TestCheckResourceAttrSet(resName, "images.0.remote.#"),
109+
resource.TestCheckResourceAttrSet(resName, "images.0.remote.0.account.#"),
110+
resource.TestCheckResourceAttrSet(resName, "images.0.remote.0.account.0.id"),
111+
resource.TestCheckResourceAttrSet(resName, "images.0.remote.0.account.0.resource_type"),
112+
),
113+
},
114+
},
115+
})
116+
}
117+
99118
func TestAccIBMISImageDataSource_With_FilterStatus(t *testing.T) {
100119
resName := "data.ibm_is_images.test1"
101120
resource.Test(t, resource.TestCase{
@@ -149,3 +168,12 @@ func testAccCheckIBMISImagesDataSourceWithStatusPublic(status string) string {
149168
}
150169
`, status)
151170
}
171+
172+
func testAccCheckIBMISImagesDataSourceWithRemoteAccountId(remoteAccountId string) string {
173+
return fmt.Sprintf(`
174+
data "ibm_is_images" "example" {
175+
remote_account_id = "%s"
176+
status = "available"
177+
}
178+
`, remoteAccountId)
179+
}

ibm/service/vpc/data_source_ibm_is_instance_profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ func instanceProfileGet(context context.Context, d *schema.ResourceData, meta in
980980

981981
// Manufacturer details added.
982982
if profile.VcpuManufacturer != nil {
983-
err = d.Set(isInstanceVCPUManufacturer, dataSourceInstanceProfileFlattenVcpuManufacture(*profile.VcpuManufacturer))
983+
err = d.Set(isInstanceVCPUManufacturer, dataSourceInstanceProfileFlattenVcpuManufacture(*profile.VcpuManufacturer.(*vpcv1.InstanceProfileVcpuManufacturer)))
984984
if err != nil {
985985
return flex.DiscriminatedTerraformErrorf(err, fmt.Sprintf("Error setting vcpu_manufacturer: %s", err), "(Data) ibm_is_instance_profile", "read", "set-vcpu_manufacturer").GetDiag()
986986
}

ibm/service/vpc/data_source_ibm_is_instance_profiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ func instanceProfilesList(context context.Context, d *schema.ResourceData, meta
952952
// reduce the line of code here. - sumit's suggestions
953953
if profile.VcpuManufacturer != nil {
954954
vcpuManufacturerList := []map[string]interface{}{}
955-
vcpuManufacturerMap := dataSourceInstanceProfileVcpuManufacturerToMap(*profile.VcpuManufacturer)
955+
vcpuManufacturerMap := dataSourceInstanceProfileVcpuManufacturerToMap(*profile.VcpuManufacturer.(*vpcv1.InstanceProfileVcpuManufacturer))
956956
vcpuManufacturerList = append(vcpuManufacturerList, vcpuManufacturerMap)
957957
l["vcpu_manufacturer"] = vcpuManufacturerList
958958
}

ibm/service/vpc/resource_ibm_is_image.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ const (
5151
isImageDeprecate = "deprecate"
5252
isImageObsolete = "obsolete"
5353
isImageUserDataFormat = "user_data_format"
54+
55+
isImageRemote = "remote"
5456
)
5557

5658
func ResourceIBMISImage() *schema.Resource {

website/docs/d/is_image.html.markdown

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Review the argument references that you can specify for your data source.
4747

4848
- `visibility` - (Optional, String) The visibility of the image. Accepted values are `public` or `private`.
4949

50+
5051
## Attribute reference
5152
In addition to all argument reference list, you can access the following attribute references after your data source is created.
5253

@@ -103,6 +104,15 @@ In addition to all argument reference list, you can access the following attribu
103104

104105
- `source_volume` - The source volume id of the image.
105106
- `user_data_format` - (String) The user data format for this image.
107+
- `remote` - (Optional, List) If present, this property indicates that the resource associated with this reference is remote and therefore may not be directly retrievable.
108+
109+
**Nested schema for `remote`:**
110+
- `account` - (Optional, List) Indicates that the referenced resource is remote to this account, and identifies the owning account.
111+
112+
**Nested schema for `account`:**
113+
- `id` – (Computed, String) The unique identifier for this account.
114+
- `resource_type` – (Computed, String) The resource type.
115+
106116

107117
~> **Note:** </br> Supported values are : </br>
108118
**&#x2022;** `cloud_init`: user_data will be interpreted according to the cloud-init standard.</br>

0 commit comments

Comments
 (0)