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

[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 (DCNE-208) #302

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
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
anvitha-jain marked this conversation as resolved.
Show resolved Hide resolved
}

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")
}
Loading