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

Enhancement: Added operating system attributes to is images data sources #4998

Merged
merged 4 commits into from
Dec 19, 2023
Merged
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
196 changes: 194 additions & 2 deletions ibm/service/vpc/data_source_ibm_is_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,107 @@ func DataSourceIBMISImage() *schema.Resource {
ValidateFunc: validate.ValidateAllowedStringValues([]string{"public", "private"}),
Description: "Whether the image is publicly visible or private to the account",
},

"resource_group": {
Type: schema.TypeList,
Computed: true,
Description: "The resource group for this IPsec policy.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this resource group.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this resource group.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this resource group.",
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of this image",
},

"status_reasons": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The reasons for the current status (if any).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A snake case string succinctly identifying the status reason.",
},
"message": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "An explanation of the status reason.",
},
"more_info": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about this status reason.",
},
},
},
},
"operating_system": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"architecture": {
Type: schema.TypeString,
Computed: true,
Description: "The operating system architecture",
},
"dedicated_host_only": {
Type: schema.TypeBool,
Computed: true,
Description: "Images with this operating system can only be used on dedicated hosts or dedicated host groups",
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Description: "A unique, display-friendly name for the operating system",
},
"family": {
Type: schema.TypeString,
Computed: true,
Description: "The software family for this operating system",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this operating system",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this operating system",
},
"vendor": {
Type: schema.TypeString,
Computed: true,
Description: "The vendor of the operating system",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "The major release version of this operating system",
},
},
},
},
"os": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -207,6 +301,9 @@ func imageGetByName(d *schema.ResourceData, meta interface{}, name, visibility s
if *image.Status == "deprecated" {
fmt.Printf("[WARN] Given image %s is deprecated and soon will be obsolete.", name)
}
if len(image.StatusReasons) > 0 {
d.Set("status_reasons", dataSourceIBMIsImageFlattenStatusReasons(image.StatusReasons))
}
d.Set("name", *image.Name)
accesstags, err := flex.GetGlobalTagsUsingCRN(meta, *image.CRN, "", isImageAccessTagType)
if err != nil {
Expand All @@ -215,6 +312,19 @@ func imageGetByName(d *schema.ResourceData, meta interface{}, name, visibility s
}
d.Set(isImageAccessTags, accesstags)
d.Set("visibility", *image.Visibility)

if image.OperatingSystem != nil {
operatingSystemList := []map[string]interface{}{}
operatingSystemMap := dataSourceIBMISImageOperatingSystemToMap(*image.OperatingSystem)
operatingSystemList = append(operatingSystemList, operatingSystemMap)
d.Set("operating_system", operatingSystemList)
}
if image.ResourceGroup != nil {
resourceGroupList := []map[string]interface{}{}
resourceGroupMap := dataSourceImageResourceGroupToMap(*image.ResourceGroup)
resourceGroupList = append(resourceGroupList, resourceGroupMap)
d.Set("resource_group", resourceGroupList)
}
d.Set("os", *image.OperatingSystem.Name)
d.Set("architecture", *image.OperatingSystem.Architecture)
d.Set("crn", *image.CRN)
Expand Down Expand Up @@ -271,8 +381,23 @@ func imageGetById(d *schema.ResourceData, meta interface{}, identifier string) e
if *image.Status == "deprecated" {
fmt.Printf("[WARN] Given image %s is deprecated and soon will be obsolete.", name)
}
if len(image.StatusReasons) > 0 {
d.Set("status_reasons", dataSourceIBMIsImageFlattenStatusReasons(image.StatusReasons))
}
d.Set("name", *image.Name)
d.Set("visibility", *image.Visibility)
if image.OperatingSystem != nil {
operatingSystemList := []map[string]interface{}{}
operatingSystemMap := dataSourceIBMISImageOperatingSystemToMap(*image.OperatingSystem)
operatingSystemList = append(operatingSystemList, operatingSystemMap)
d.Set("operating_system", operatingSystemList)
}
if image.ResourceGroup != nil {
resourceGroupList := []map[string]interface{}{}
resourceGroupMap := dataSourceImageResourceGroupToMap(*image.ResourceGroup)
resourceGroupList = append(resourceGroupList, resourceGroupMap)
d.Set("resource_group", resourceGroupList)
}
d.Set("os", *image.OperatingSystem.Name)
d.Set("architecture", *image.OperatingSystem.Architecture)
d.Set("crn", *image.CRN)
Expand All @@ -297,6 +422,36 @@ func imageGetById(d *schema.ResourceData, meta interface{}, identifier string) e
return nil
}

func dataSourceIBMISImageOperatingSystemToMap(operatingSystemItem vpcv1.OperatingSystem) (operatingSystemMap map[string]interface{}) {
operatingSystemMap = map[string]interface{}{}

if operatingSystemItem.Architecture != nil {
operatingSystemMap["architecture"] = operatingSystemItem.Architecture
}
if operatingSystemItem.DedicatedHostOnly != nil {
operatingSystemMap["dedicated_host_only"] = operatingSystemItem.DedicatedHostOnly
}
if operatingSystemItem.DisplayName != nil {
operatingSystemMap["display_name"] = operatingSystemItem.DisplayName
}
if operatingSystemItem.Family != nil {
operatingSystemMap["family"] = operatingSystemItem.Family
}
if operatingSystemItem.Href != nil {
operatingSystemMap["href"] = operatingSystemItem.Href
}
if operatingSystemItem.Name != nil {
operatingSystemMap["name"] = operatingSystemItem.Name
}
if operatingSystemItem.Vendor != nil {
operatingSystemMap["vendor"] = operatingSystemItem.Vendor
}
if operatingSystemItem.Version != nil {
operatingSystemMap["version"] = operatingSystemItem.Version
}
return operatingSystemMap
}

func dataSourceImageCollectionCatalogOfferingToMap(imageCatalogOfferingItem vpcv1.ImageCatalogOffering) (imageCatalogOfferingMap map[string]interface{}) {
imageCatalogOfferingMap = map[string]interface{}{}
if imageCatalogOfferingItem.Managed != nil {
Expand All @@ -320,3 +475,40 @@ func dataSourceImageCollectionCatalogOfferingToMap(imageCatalogOfferingItem vpcv

return imageCatalogOfferingMap
}

func dataSourceIBMIsImageFlattenStatusReasons(result []vpcv1.ImageStatusReason) (statusReasons []map[string]interface{}) {
for _, statusReasonsItem := range result {
statusReasons = append(statusReasons, dataSourceIBMIsImageStatusReasonToMap(&statusReasonsItem))
}

return statusReasons
}

func dataSourceIBMIsImageStatusReasonToMap(model *vpcv1.ImageStatusReason) map[string]interface{} {
modelMap := make(map[string]interface{})
if model.Code != nil {
modelMap["code"] = *model.Code
}
if model.Message != nil {
modelMap["message"] = *model.Message
}
if model.MoreInfo != nil {
modelMap["more_info"] = *model.MoreInfo
}
return modelMap
}
func dataSourceImageResourceGroupToMap(resourceGroupItem vpcv1.ResourceGroupReference) (resourceGroupMap map[string]interface{}) {
resourceGroupMap = map[string]interface{}{}

if resourceGroupItem.Href != nil {
resourceGroupMap["href"] = resourceGroupItem.Href
}
if resourceGroupItem.ID != nil {
resourceGroupMap["id"] = resourceGroupItem.ID
}
if resourceGroupItem.Name != nil {
resourceGroupMap["name"] = resourceGroupItem.Name
}

return resourceGroupMap
}
37 changes: 37 additions & 0 deletions ibm/service/vpc/data_source_ibm_is_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,33 @@ func TestAccIBMISImageDataSource_basic(t *testing.T) {
},
})
}
func TestAccIBMISImageDataSource_All(t *testing.T) {
resName := "data.ibm_is_image.test1"
imageName := fmt.Sprintf("tfimage-name-%d", acctest.RandIntRange(10, 100))

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISImageDataSourceAllConfig(imageName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resName, "operating_system.0.name"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.dedicated_host_only"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.display_name"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.family"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.href"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.vendor"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.version"),
resource.TestCheckResourceAttrSet(resName, "operating_system.0.architecture"),
resource.TestCheckResourceAttrSet(resName, "status"),
resource.TestCheckResourceAttrSet(resName, "resource_group.0.id"),
resource.TestCheckResourceAttrSet(resName, "resource_group.0.name"),
),
},
},
})
}
func TestAccIBMISImageDataSource_ilc(t *testing.T) {
resName := "data.ibm_is_image.test1"
imageName := fmt.Sprintf("tfimage-name-%d", acctest.RandIntRange(10, 100))
Expand Down Expand Up @@ -144,6 +171,16 @@ func testAccCheckIBMISImageDataSourceConfig(imageName string) string {
}`, acc.Image_cos_url, imageName, acc.Image_operating_system)
}

func testAccCheckIBMISImageDataSourceAllConfig(imageName string) string {
return fmt.Sprintf(`
data "ibm_is_images" "test1" {
status = "available"
}
data "ibm_is_image" "test1" {
name = data.ibm_is_images.test1.images.0.name
}`)
}

func testAccCheckIBMISImageDataSourceConfigIlc(imageName string) string {
return fmt.Sprintf(`
resource "ibm_is_image" "isExampleImage" {
Expand Down
Loading
Loading