Skip to content

Commit

Permalink
[minor_change] Optimization through an additional api call at list-id…
Browse files Browse the repository at this point in the history
…entity schema endpoint to avoid retrieval of all schemas in order to get the id of a schema
  • Loading branch information
akinross committed Oct 15, 2024
1 parent b5699dd commit 8770ca1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
47 changes: 30 additions & 17 deletions mso/datasource_mso_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
Expand Down
19 changes: 19 additions & 0 deletions mso/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}

0 comments on commit 8770ca1

Please sign in to comment.