From e30b5c61e17bf8614ef5f929f8a60761924a14c2 Mon Sep 17 00:00:00 2001 From: Ben Buchanan Date: Thu, 26 Sep 2024 18:02:43 -0400 Subject: [PATCH] feat(Catalog Management): support new offering version limit --- go.mod | 4 +- go.sum | 7 +- .../data_source_ibm_cm_offering.go | 2 +- .../resource_ibm_cm_offering.go | 4 +- .../resource_ibm_cm_version.go | 212 ++++++++---------- ibm/service/catalogmanagement/utils.go | 71 ++++++ website/docs/r/cm_version.html.markdown | 1 + 7 files changed, 181 insertions(+), 120 deletions(-) diff --git a/go.mod b/go.mod index c6afce8fcf..096ed2a443 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/IBM/eventstreams-go-sdk v1.4.0 github.com/IBM/go-sdk-core v1.1.0 github.com/IBM/go-sdk-core/v3 v3.2.4 - github.com/IBM/go-sdk-core/v5 v5.17.4 + github.com/IBM/go-sdk-core/v5 v5.17.5 github.com/IBM/ibm-cos-sdk-go v1.10.3 github.com/IBM/ibm-cos-sdk-go-config/v2 v2.1.0 github.com/IBM/ibm-hpcs-tke-sdk v0.0.0-20211109141421-a4b61b05f7d1 @@ -30,7 +30,7 @@ require ( github.com/IBM/logs-router-go-sdk v1.0.5 github.com/IBM/mqcloud-go-sdk v0.1.0 github.com/IBM/networking-go-sdk v0.49.0 - github.com/IBM/platform-services-go-sdk v0.68.1 + github.com/IBM/platform-services-go-sdk v0.69.1 github.com/IBM/project-go-sdk v0.3.5 github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5 github.com/IBM/sarama v1.41.2 diff --git a/go.sum b/go.sum index 44d4ff9877..0df522c953 100644 --- a/go.sum +++ b/go.sum @@ -154,8 +154,9 @@ github.com/IBM/go-sdk-core/v5 v5.6.3/go.mod h1:tt/B9rxLkRtglE7pvqLuYikgCXaZFL3bt github.com/IBM/go-sdk-core/v5 v5.7.0/go.mod h1:+YbdhrjCHC84ls4MeBp+Hj4NZCni+tDAc0XQUqRO9Jc= github.com/IBM/go-sdk-core/v5 v5.9.5/go.mod h1:YlOwV9LeuclmT/qi/LAK2AsobbAP42veV0j68/rlZsE= github.com/IBM/go-sdk-core/v5 v5.10.2/go.mod h1:WZPFasUzsKab/2mzt29xPcfruSk5js2ywAPwW4VJjdI= -github.com/IBM/go-sdk-core/v5 v5.17.4 h1:VGb9+mRrnS2HpHZFM5hy4J6ppIWnwNrw0G+tLSgcJLc= github.com/IBM/go-sdk-core/v5 v5.17.4/go.mod h1:KsAAI7eStAWwQa4F96MLy+whYSh39JzNjklZRbN/8ns= +github.com/IBM/go-sdk-core/v5 v5.17.5 h1:AjGC7xNee5tgDIjndekBDW5AbypdERHSgib3EZ1KNsA= +github.com/IBM/go-sdk-core/v5 v5.17.5/go.mod h1:KsAAI7eStAWwQa4F96MLy+whYSh39JzNjklZRbN/8ns= github.com/IBM/ibm-cos-sdk-go v1.10.3 h1:YfZSLqMiCrqDPbr3r+amY2sicIXlrd+3L5pok6QRXIQ= github.com/IBM/ibm-cos-sdk-go v1.10.3/go.mod h1:T9x7pC47DUd5jD/TMFzlvly39P6EdW5wOemA78XEo2g= github.com/IBM/ibm-cos-sdk-go-config/v2 v2.1.0 h1:U7EmXSfv7jtugRpTpOkPUmgS/xiNKtGfKVH3BGyC1hg= @@ -175,8 +176,8 @@ github.com/IBM/mqcloud-go-sdk v0.1.0 h1:fWt4uisg5GbbsfNmAxx5/6c5gQIPM+VrEsTtnimE github.com/IBM/mqcloud-go-sdk v0.1.0/go.mod h1:LesMQlKHXvdks4jqQLZH7HfATY5lvTzHuwQU5+y7b2g= github.com/IBM/networking-go-sdk v0.49.0 h1:lPS34u3C0JVrbxH+Ulua76Nwl6Frv8BEfq6LRkyvOv0= github.com/IBM/networking-go-sdk v0.49.0/go.mod h1:G9CKbmPE8gSLjN+ABh4hIZ1bMx076enl5Eekvj6zQnA= -github.com/IBM/platform-services-go-sdk v0.68.1 h1:RXGzEmdllzSj5OzCJO7AoTeQ+cgTTNa20CrrpLQe5KQ= -github.com/IBM/platform-services-go-sdk v0.68.1/go.mod h1:6rYd3stLSnotYmZlxclw45EJPaQuLmh5f7c+Mg7rOg4= +github.com/IBM/platform-services-go-sdk v0.69.1 h1:Wb8BYVpsPIppWbOQCgF7ytm+BbSOXdWWCf9zcZ6xGA4= +github.com/IBM/platform-services-go-sdk v0.69.1/go.mod h1:ZP3zUDxR1qRdUqzFdnJOlQN0QpVYol2eOUCv4uk03Jc= github.com/IBM/project-go-sdk v0.3.5 h1:L+YClFUa14foS0B/hOOY9n7sIdsT5/XQicnXOyJSpyM= github.com/IBM/project-go-sdk v0.3.5/go.mod h1:FOJM9ihQV3EEAY6YigcWiTNfVCThtdY8bLC/nhQHFvo= github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5 h1:NPUhkoOCRuv3OFWt19PmwjXGGTKlvmbuPg9fUrBUNe4= diff --git a/ibm/service/catalogmanagement/data_source_ibm_cm_offering.go b/ibm/service/catalogmanagement/data_source_ibm_cm_offering.go index beba0f75a0..23e2c30a3b 100644 --- a/ibm/service/catalogmanagement/data_source_ibm_cm_offering.go +++ b/ibm/service/catalogmanagement/data_source_ibm_cm_offering.go @@ -2450,7 +2450,7 @@ func dataSourceIBMCmOfferingRead(context context.Context, d *schema.ResourceData getOfferingOptions.SetCatalogIdentifier(d.Get("catalog_id").(string)) getOfferingOptions.SetOfferingID(d.Get("offering_id").(string)) - offering, response, err := catalogManagementClient.GetOfferingWithContext(context, getOfferingOptions) + offering, response, err := FetchOfferingWithAllVersions(context, catalogManagementClient, getOfferingOptions) if err != nil { tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetOfferingWithContext failed %s\n%s", err, response), "(Data) ibm_cm_object", "read") log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) diff --git a/ibm/service/catalogmanagement/resource_ibm_cm_offering.go b/ibm/service/catalogmanagement/resource_ibm_cm_offering.go index 556226e8c8..40b1ee1a82 100644 --- a/ibm/service/catalogmanagement/resource_ibm_cm_offering.go +++ b/ibm/service/catalogmanagement/resource_ibm_cm_offering.go @@ -2559,6 +2559,8 @@ func resourceIBMCmOfferingCreate(context context.Context, d *schema.ResourceData getOfferingOptions.SetCatalogIdentifier(d.Get("catalog_id").(string)) getOfferingOptions.SetOfferingID(d.Get("offering_id").(string)) + // Don't need to get full offering with all versions here because we call resourceIBMCmOfferingRead which does + // This get is just to see if the offering exists and set state id offering, response, err := catalogManagementClient.GetOfferingWithContext(context, getOfferingOptions) if err != nil { if response != nil && response.StatusCode == 404 { @@ -2817,7 +2819,7 @@ func resourceIBMCmOfferingRead(context context.Context, d *schema.ResourceData, getOfferingOptions.SetCatalogIdentifier(d.Get("catalog_id").(string)) getOfferingOptions.SetOfferingID(d.Id()) - offering, response, err := catalogManagementClient.GetOfferingWithContext(context, getOfferingOptions) + offering, response, err := FetchOfferingWithAllVersions(context, catalogManagementClient, getOfferingOptions) if err != nil { if response != nil && response.StatusCode == 404 { d.SetId("") diff --git a/ibm/service/catalogmanagement/resource_ibm_cm_version.go b/ibm/service/catalogmanagement/resource_ibm_cm_version.go index 60cd65cf94..79d4fc6455 100644 --- a/ibm/service/catalogmanagement/resource_ibm_cm_version.go +++ b/ibm/service/catalogmanagement/resource_ibm_cm_version.go @@ -10,6 +10,7 @@ import ( "log" "strconv" "strings" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -1069,6 +1070,7 @@ func ResourceIBMCmVersion() *schema.Resource { }, "long_description": &schema.Schema{ Type: schema.TypeString, + Optional: true, Computed: true, Description: "Long description for version.", }, @@ -1815,25 +1817,23 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, getOfferingOptions := &catalogmanagementv1.GetOfferingOptions{} getOfferingOptions.SetCatalogIdentifier(d.Get("catalog_id").(string)) getOfferingOptions.SetOfferingID(d.Get("offering_id").(string)) - oldOffering, response, err := catalogManagementClient.GetOfferingWithContext(context, getOfferingOptions) + + offering, response, err := catalogManagementClient.ImportOfferingVersionWithContext(context, importOfferingVersionOptions) if err != nil { - if response != nil && response.StatusCode == 404 { - d.SetId("") - return nil - } - tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetOfferingWithContext failed %s\n%s", err, response), "ibm_cm_version", "create") + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ImportOfferingVersionWithContext failed %s\n%s", err, response), "ibm_cm_version", "create") log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) return tfErr.GetDiag() } - offering, response, err := catalogManagementClient.ImportOfferingVersionWithContext(context, importOfferingVersionOptions) + // Have to fetch all offering versions to find latest one in case this is XL offering + offering, response, err = FetchOfferingWithAllVersions(context, catalogManagementClient, getOfferingOptions) if err != nil { - tfErr := flex.TerraformErrorf(err, fmt.Sprintf("ImportOfferingVersionWithContext failed %s\n%s", err, response), "ibm_cm_version", "create") + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetOfferingWithContext failed %s\n%s", err, response), "(Data) ibm_cm_object", "read") log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) return tfErr.GetDiag() } - activeVersion, err := getVersionFromOffering(oldOffering, offering) + activeVersion, err := getLatestVersionFromOffering(offering) if err != nil { tfErr := flex.TerraformErrorf(err, fmt.Sprintf("getVersionFromOffering failed %s\n%s", err, response), "ibm_cm_version", "create") log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) @@ -1842,33 +1842,14 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, d.SetId(fmt.Sprintf("%s/%s", *offering.CatalogID, *activeVersion.ID)) - updateOfferingOptions := &catalogmanagementv1.UpdateOfferingOptions{} + patchUpdateVersionOptions := &catalogmanagementv1.PatchUpdateVersionOptions{} - updateOfferingOptions.SetCatalogIdentifier(*offering.CatalogID) - updateOfferingOptions.SetOfferingID(*offering.ID) + patchUpdateVersionOptions.SetVersionLocID(*activeVersion.VersionLocator) ifMatch := fmt.Sprintf("\"%s\"", *offering.Rev) - updateOfferingOptions.IfMatch = &ifMatch + patchUpdateVersionOptions.IfMatch = &ifMatch hasChange := false - - // find kind and version index - var kindIndex int - var versionIndex int - for i, kind := range offering.Kinds { - if *kind.ID == *activeVersion.KindID { - kindIndex = i - - if kind.Versions != nil && len(kind.Versions) > 0 { - for j, version := range kind.Versions { - if *version.ID == *activeVersion.ID { - versionIndex = j - } - } - } - } - } - - pathToVersion := fmt.Sprintf("/kinds/%d/versions/%d", kindIndex, versionIndex) + pathToVersion := "/kinds/0/versions/0" if _, ok := d.GetOk("tags"); ok { var method string @@ -1883,7 +1864,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("tags"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("configuration"); ok { @@ -1905,7 +1886,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: configurations, } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("iam_permissions"); ok { @@ -1921,7 +1902,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("iam_permissions"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("required_resources"); ok { @@ -1937,7 +1918,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("required_resources"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("install"); ok { @@ -1953,7 +1934,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("install.0"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("pre_install"); ok { @@ -1969,7 +1950,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("pre_install"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("licenses"); ok { @@ -1985,7 +1966,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("licenses"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("solution_info"); ok { @@ -2007,7 +1988,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: solutionInfoMap, } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("usage"); ok { @@ -2023,7 +2004,7 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("usage"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if _, ok := d.GetOk("terraform_version"); ok { @@ -2039,14 +2020,30 @@ func resourceIBMCmVersionCreate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("terraform_version"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) + hasChange = true + } + if d.HasChange("long_description") { + var method string + if activeVersion.LongDescription == nil { + method = "add" + } else { + method = "replace" + } + path := fmt.Sprintf("%s/long_description", pathToVersion) + update := catalogmanagementv1.JSONPatchOperation{ + Op: &method, + Path: &path, + Value: d.Get("long_description"), + } + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if hasChange { - _, response, err := catalogManagementClient.UpdateOfferingWithContext(context, updateOfferingOptions) + _, response, err := catalogManagementClient.PatchUpdateVersionWithContext(context, patchUpdateVersionOptions) if err != nil { - tfErr := flex.TerraformErrorf(err, fmt.Sprintf("UpdateOfferingWithContext failed %s\n%s", err, response), "ibm_cm_version", "create") + tfErr := flex.TerraformErrorf(err, fmt.Sprintf("PatchUpdateVersionWithContext failed %s\n%s", err, response), "ibm_cm_version", "create") log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) return tfErr.GetDiag() } @@ -2449,48 +2446,14 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, } activeVersion := partialOffering.Kinds[0].Versions[0] - getOfferingOptions := &catalogmanagementv1.GetOfferingOptions{} - - getOfferingOptions.SetCatalogIdentifier(*partialOffering.CatalogID) - getOfferingOptions.SetOfferingID(*partialOffering.ID) - offering, response, err := catalogManagementClient.GetOfferingWithContext(context, getOfferingOptions) - if err != nil { - if response != nil && response.StatusCode == 404 { - d.SetId("") - return nil - } - tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetOfferingWithContext failed %s\n%s", err, response), "ibm_cm_version", "update") - log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) - return tfErr.GetDiag() - } - - updateOfferingOptions := &catalogmanagementv1.UpdateOfferingOptions{} + patchUpdateVersionOptions := &catalogmanagementv1.PatchUpdateVersionOptions{} - updateOfferingOptions.SetCatalogIdentifier(*offering.CatalogID) - updateOfferingOptions.SetOfferingID(*offering.ID) - ifMatch := fmt.Sprintf("\"%s\"", *offering.Rev) - updateOfferingOptions.IfMatch = &ifMatch + patchUpdateVersionOptions.SetVersionLocID(*activeVersion.VersionLocator) + ifMatch := fmt.Sprintf("\"%s\"", *partialOffering.Rev) + patchUpdateVersionOptions.IfMatch = &ifMatch hasChange := false - - // find kind and version index - var kindIndex int - var versionIndex int - for i, kind := range offering.Kinds { - if *kind.ID == *activeVersion.KindID { - kindIndex = i - - if kind.Versions != nil && len(kind.Versions) > 0 { - for j, version := range kind.Versions { - if *version.ID == *activeVersion.ID { - versionIndex = j - } - } - } - } - } - - pathToVersion := fmt.Sprintf("/kinds/%d/versions/%d", kindIndex, versionIndex) + pathToVersion := "/kinds/0/versions/0" if d.HasChange("flavor") { var method string @@ -2505,7 +2468,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("flavor.0"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("tags") { @@ -2521,7 +2484,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("tags"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("configuration") { @@ -2543,7 +2506,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: configurations, } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("iam_permissions") { @@ -2559,7 +2522,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("iam_permissions"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("required_resources") { @@ -2575,7 +2538,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("required_resources"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("install") { @@ -2591,7 +2554,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("install.0"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("pre_install") { @@ -2607,7 +2570,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("pre_install"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("licenses") { @@ -2623,7 +2586,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("licenses"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("solution_info") { @@ -2645,7 +2608,7 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: solutionInfoMap, } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if d.HasChange("terraform_version") { @@ -2661,12 +2624,28 @@ func resourceIBMCmVersionUpdate(context context.Context, d *schema.ResourceData, Path: &path, Value: d.Get("terraform_version"), } - updateOfferingOptions.Updates = append(updateOfferingOptions.Updates, update) + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) + hasChange = true + } + if d.HasChange("long_description") { + var method string + if activeVersion.LongDescription == nil { + method = "add" + } else { + method = "replace" + } + path := fmt.Sprintf("%s/long_description", pathToVersion) + update := catalogmanagementv1.JSONPatchOperation{ + Op: &method, + Path: &path, + Value: d.Get("long_description"), + } + patchUpdateVersionOptions.Updates = append(patchUpdateVersionOptions.Updates, update) hasChange = true } if hasChange { - _, response, err := catalogManagementClient.UpdateOfferingWithContext(context, updateOfferingOptions) + _, response, err := catalogManagementClient.PatchUpdateVersionWithContext(context, patchUpdateVersionOptions) if err != nil { tfErr := flex.TerraformErrorf(err, fmt.Sprintf("UpdateOfferingWithContext failed %s\n%s", err, response), "ibm_cm_version", "update") log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage()) @@ -3543,29 +3522,36 @@ func resourceIBMCmVersionDependencyToMap(model *catalogmanagementv1.OfferingRefe return modelMap, nil } -func getVersionFromOffering(oldOffering *catalogmanagementv1.Offering, newOffering *catalogmanagementv1.Offering) (*catalogmanagementv1.Version, error) { - var oldVersionList []catalogmanagementv1.Version - var newVersionList []catalogmanagementv1.Version - - for _, kind := range oldOffering.Kinds { - oldVersionList = append(oldVersionList, kind.Versions...) +// Loop over all versions to find the one with the latest created time +func getLatestVersionFromOffering(offering *catalogmanagementv1.Offering) (*catalogmanagementv1.Version, error) { + if offering == nil { + return nil, fmt.Errorf("offering is nil") } - for _, kind := range newOffering.Kinds { - newVersionList = append(newVersionList, kind.Versions...) - } + var latestVersion *catalogmanagementv1.Version + var latestTime time.Time - for _, newVer := range newVersionList { - isOld := false - for _, oldVer := range oldVersionList { - if *newVer.ID == *oldVer.ID { - isOld = true - break + // Iterate over all kinds + for _, kind := range offering.Kinds { + // Iterate over all versions + for _, version := range kind.Versions { + if version.Created == nil { + continue + } + + createdTime := time.Time(*version.Created) + + // Check if this version is the latest one + if latestVersion == nil || createdTime.After(latestTime) { + latestVersion = &version + latestTime = createdTime } } - if !isOld { - return &newVer, nil - } } - return nil, flex.FmtErrorf("error finding imported version") + + if latestVersion == nil { + return nil, fmt.Errorf("no versions found in offering") + } + + return latestVersion, nil } diff --git a/ibm/service/catalogmanagement/utils.go b/ibm/service/catalogmanagement/utils.go index c7ad144953..cb89faaa5e 100644 --- a/ibm/service/catalogmanagement/utils.go +++ b/ibm/service/catalogmanagement/utils.go @@ -1,5 +1,13 @@ package catalogmanagement +import ( + "context" + "fmt" + + "github.com/IBM/go-sdk-core/v5/core" + "github.com/IBM/platform-services-go-sdk/catalogmanagementv1" +) + func SIToSS(i []interface{}) []string { var ss []string for _, iface := range i { @@ -7,3 +15,66 @@ func SIToSS(i []interface{}) []string { } return ss } + +// FetchOfferingWithAllVersions fetches an offering and its additional versions +func FetchOfferingWithAllVersions(ctx context.Context, client *catalogmanagementv1.CatalogManagementV1, getOfferingOptions *catalogmanagementv1.GetOfferingOptions) (*catalogmanagementv1.Offering, *core.DetailedResponse, error) { + offering, response, err := client.GetOfferingWithContext(ctx, getOfferingOptions) + if err != nil { + return offering, response, err + } + + // Fetch additional versions for the offering + if err := FetchAdditionalVersions(ctx, client, offering); err != nil { + return offering, response, err + } + + return offering, response, nil +} + +// FetchAdditionalVersions fetches all versions for each kind in the offering +func FetchAdditionalVersions(ctx context.Context, client *catalogmanagementv1.CatalogManagementV1, offering *catalogmanagementv1.Offering) error { + // Define the recursive function to fetch versions for a kind + var fetchVersionsForKind func(kind *catalogmanagementv1.Kind, start *string) error + + fetchVersionsForKind = func(kind *catalogmanagementv1.Kind, start *string) error { + fmt.Println("START STRING: ", start) + // Fetch versions for the kind + getVersionsOptions := &catalogmanagementv1.GetVersionsOptions{ + CatalogIdentifier: offering.CatalogID, + OfferingID: offering.ID, + KindID: kind.ID, + Start: start, + } + + result, _, err := client.GetVersionsWithContext(ctx, getVersionsOptions) + if err != nil { + return err + } + + if result.Versions != nil { + // Append fetched versions to the kind + kind.Versions = append(kind.Versions, result.Versions...) + } + + // Check if there are more versions to fetch + if result.Next != nil && result.Next.Start != nil && *result.Next.Start != "" { + // If there are more versions to fetch, call recursively + return fetchVersionsForKind(kind, result.Next.Start) + } + + return nil + } + + // Iterate over kinds in the offering and fetch additional versions + for i := range offering.Kinds { + kind := &offering.Kinds[i] // Use a pointer to modify the original kind + if kind.AllVersions != nil && kind.AllVersions.Next != nil && kind.AllVersions.Next.Start != nil && *kind.AllVersions.Next.Start != "" { + // Load additional versions for this kind + if err := fetchVersionsForKind(kind, kind.AllVersions.Next.Start); err != nil { + return err + } + } + } + + return nil +} diff --git a/website/docs/r/cm_version.html.markdown b/website/docs/r/cm_version.html.markdown index db045e95f4..b04b0ef8ca 100644 --- a/website/docs/r/cm_version.html.markdown +++ b/website/docs/r/cm_version.html.markdown @@ -152,6 +152,7 @@ Nested scheme for **licenses**: * `name` - (Optional, String) license name. * `type` - (Optional, String) type of license e.g., Apache xxx. * `url` - (Optional, String) URL for the license text. +* `long_description` - (Optional, String) Long description for the version. * `name` - (Optional, Forces new resource, String) Name of version. Required for virtual server image for VPC. * `offering_id` - (Required, Forces new resource, String) Offering identification. * `pre_install` - (Optional, List) Optional pre-install instructions.