Skip to content

Commit

Permalink
[bugfix] Optimization through an additional api call at list-identity…
Browse files Browse the repository at this point in the history
… schema endpoint to avoid retrieval of all schemas in order to get the id of a schema (#302)
  • Loading branch information
akinross authored Oct 17, 2024
1 parent 74ff037 commit f974b5f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
44 changes: 29 additions & 15 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,39 @@ func datasourceMSOSchemaRead(d *schema.ResourceData, m interface{}) error {
msoClient := m.(*client.Client)

name := d.Get("name").(string)
con, err := msoClient.GetViaURL("api/v1/schemas")

schemaId, err := getSchemaIdFromName(msoClient, name)

var dataCon *container.Container
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
} else if schemaId != "" {
dataCon, err = msoClient.GetViaURL(fmt.Sprintf("api/v1/schemas/%s", schemaId))
if err != nil {
return err
}
count = count + 1
}
if flag != true {
return fmt.Errorf("Schema of specified name not found")
} else {
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
}
count = count + 1
}
if flag != true {
return fmt.Errorf("Schema of specified name not found")
}
dataCon = con.S("schemas").Index(count)
}

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
20 changes: 20 additions & 0 deletions mso/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,23 @@ func doPatchRequest(msoClient *client.Client, path string, payloadCon *container

return nil
}

func getSchemaIdFromName(msoClient *client.Client, name string) (string, error) {

con, err := msoClient.GetViaURL("/api/v1/schemas/list-identity")

if err != nil {
return "", nil
}

schemas := con.S("schemas").Data().([]interface{})
for _, schema := range schemas {
if displayName, ok := schema.(map[string]interface{})["displayName"]; ok && displayName == name {
if id, ok := schema.(map[string]interface{})["id"]; ok {
return id.(string), nil
}
}
}

return "", fmt.Errorf("Schema of specified name not found")
}

0 comments on commit f974b5f

Please sign in to comment.