diff --git a/import_account_schema_attribute.go b/import_account_schema_attribute.go index b8da408..05687f3 100644 --- a/import_account_schema_attribute.go +++ b/import_account_schema_attribute.go @@ -3,7 +3,13 @@ package main import "github.com/hashicorp/terraform-plugin-sdk/helper/schema" func resourceAccountSchemaImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - err := resourceAccountSchemaRead(d, meta) + sourceID, schemaId, err := splitAccountSchemaID(d.Id()) + if err != nil { + return []*schema.ResourceData{}, err + } + d.Set("source_id", sourceID) + d.Set("schema_id", schemaId) + err = resourceAccountSchemaRead(d, meta) if err != nil { return []*schema.ResourceData{}, err } diff --git a/import_resource_role.go b/import_resource_role.go new file mode 100644 index 0000000..837ceac --- /dev/null +++ b/import_resource_role.go @@ -0,0 +1,12 @@ +package main + +import "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + +func resourceRoleImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + err := resourceRoleRead(d, meta) + if err != nil { + return []*schema.ResourceData{}, err + } + + return []*schema.ResourceData{d}, nil +} diff --git a/resource_account_schema.go b/resource_account_schema.go index a88d53e..e4293c8 100644 --- a/resource_account_schema.go +++ b/resource_account_schema.go @@ -27,8 +27,6 @@ func resourceAccountSchemaCreate(d *schema.ResourceData, m interface{}) error { return err } - log.Printf("[INFO] Creating Account Schema Attribute %v", accountSchema.Attributes) - client, err := m.(*Config).IdentityNowClient() if err != nil { return err @@ -48,6 +46,21 @@ func resourceAccountSchemaCreate(d *schema.ResourceData, m interface{}) error { for i := range accountSchema.Attributes { newAccountSchema.Attributes = append(newAccountSchema.Attributes, accountSchema.Attributes[i]) } + seen := make(map[*AccountSchemaAttribute]bool) + result := []*AccountSchemaAttribute{} + + // Loop through the slice, adding elements to the map if they haven't been seen before + for _, val := range newAccountSchema.Attributes { + if _, ok := seen[val]; !ok { + seen[val] = true + result = append(result, val) + fmt.Printf("seen: %v", seen) + } + } + newAccountSchema.Attributes = result + newAccountSchema.ID = accountSchema.SourceID + log.Printf("[INFO] Creating Account Schema Attribute for source %+v\n", newAccountSchema.SourceID) + accountSchemaResponse, err := client.UpdateAccountSchema(context.Background(), newAccountSchema) if err != nil { return err diff --git a/resource_role.go b/resource_role.go index b38f476..b175ced 100644 --- a/resource_role.go +++ b/resource_role.go @@ -14,6 +14,10 @@ func resourceRole() *schema.Resource { Update: resourceRoleUpdate, Delete: resourceRoleDelete, + Importer: &schema.ResourceImporter{ + State: resourceRoleImport, + }, + Schema: roleFields(), } } diff --git a/util.go b/util.go index 3d1ab4f..aae0e7f 100644 --- a/util.go +++ b/util.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/url" + "strings" ) func toArrayInterface(in []string) []interface{} { @@ -105,3 +106,12 @@ func setPasswordPolicyUrlValues(attributes *PasswordPolicy) (url.Values, error) } return data, nil } +func splitAccountSchemaID(id string) (sourceId string, schemaId string, err error) { + separator := "-" + + result := strings.Split(id, separator) + if len(result) == 2 { + return result[0], result[1], nil + } + return "", "", fmt.Errorf("[ERROR Getting source id and name. id: %s", id) +}