From 8770ca1a7c21d2638c62ae772f60b6668589bc7d Mon Sep 17 00:00:00 2001 From: akinross Date: Tue, 15 Oct 2024 11:07:02 +0200 Subject: [PATCH] [minor_change] Optimization through an additional api call at list-identity schema endpoint to avoid retrieval of all schemas in order to get the id of a schema --- mso/datasource_mso_schema.go | 47 +++++++++++++++++++++++------------- mso/utils.go | 19 +++++++++++++++ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/mso/datasource_mso_schema.go b/mso/datasource_mso_schema.go index 7d7e6cfc..ac3e14ef 100644 --- a/mso/datasource_mso_schema.go +++ b/mso/datasource_mso_schema.go @@ -5,6 +5,7 @@ import ( "log" "github.com/ciscoecosystem/mso-go-client/client" + "github.com/ciscoecosystem/mso-go-client/container" "github.com/ciscoecosystem/mso-go-client/models" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" @@ -76,26 +77,38 @@ func datasourceMSOSchemaRead(d *schema.ResourceData, m interface{}) error { msoClient := m.(*client.Client) name := d.Get("name").(string) - con, err := msoClient.GetViaURL("api/v1/schemas") - if err != nil { - return err - } - data := con.S("schemas").Data().([]interface{}) - var flag bool - var count int - for _, info := range data { - val := info.(map[string]interface{}) - if val["displayName"].(string) == name { - flag = true - break + + schemaId := getSchemaIdFromName(msoClient, name) + + var dataCon *container.Container + if schemaId == "" { + con, err := msoClient.GetViaURL("api/v1/schemas") + if err != nil { + return err } - count = count + 1 - } - if flag != true { - return fmt.Errorf("Schema of specified name not found") + data := con.S("schemas").Data().([]interface{}) + var flag bool + var count int + for _, info := range data { + val := info.(map[string]interface{}) + if val["displayName"].(string) == name { + flag = true + break + } + count = count + 1 + } + if flag != true { + return fmt.Errorf("Schema of specified name not found") + } + dataCon = con.S("schemas").Index(count) + } else { + con, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaId)) + if err != nil { + return err + } + dataCon = con } - dataCon := con.S("schemas").Index(count) d.SetId(models.StripQuotes(dataCon.S("id").String())) d.Set("name", models.StripQuotes(dataCon.S("displayName").String())) d.Set("description", models.StripQuotes(dataCon.S("description").String())) diff --git a/mso/utils.go b/mso/utils.go index 8e3a65cd..9d6d6fb7 100644 --- a/mso/utils.go +++ b/mso/utils.go @@ -220,3 +220,22 @@ func doPatchRequest(msoClient *client.Client, path string, payloadCon *container return nil } + +func getSchemaIdFromName(msoClient *client.Client, name string) string { + + con, err := msoClient.GetViaURL("/api/v1/schemas/list-identity") + + if err != nil { + return "" + } + + schemas := con.S("schemas").Data().([]interface{}) + + for _, schema := range schemas { + if schema.(map[string]interface{})["displayName"] == name { + return schema.(map[string]interface{})["id"].(string) + } + } + + return "" +}