Skip to content

Commit

Permalink
Merge pull request #17 from amouhadi/add-import-for-roles
Browse files Browse the repository at this point in the history
add role importer
  • Loading branch information
locmai authored Nov 15, 2023
2 parents 2b495c7 + fbdf9c5 commit a87e31c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
8 changes: 7 additions & 1 deletion import_account_schema_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions import_resource_role.go
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 15 additions & 2 deletions resource_account_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions resource_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ func resourceRole() *schema.Resource {
Update: resourceRoleUpdate,
Delete: resourceRoleDelete,

Importer: &schema.ResourceImporter{
State: resourceRoleImport,
},

Schema: roleFields(),
}
}
Expand Down
10 changes: 10 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/url"
"strings"
)

func toArrayInterface(in []string) []interface{} {
Expand Down Expand Up @@ -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)
}

0 comments on commit a87e31c

Please sign in to comment.