-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource 'azuread_application_password' (#71)
- Loading branch information
Showing
16 changed files
with
812 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
package graph | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" | ||
"github.com/Azure/go-autorest/autorest/date" | ||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/p" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate" | ||
) | ||
|
||
// valid types are `application` and `service_principal` | ||
func PasswordResourceSchema(object_type string) map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
object_type + "_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.UUID, | ||
}, | ||
|
||
"key_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.UUID, | ||
}, | ||
|
||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Sensitive: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"start_date": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.ValidateRFC3339TimeString, | ||
}, | ||
|
||
"end_date": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"end_date_relative"}, | ||
ValidateFunc: validation.ValidateRFC3339TimeString, | ||
}, | ||
|
||
"end_date_relative": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ConflictsWith: []string{"end_date"}, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
} | ||
} | ||
|
||
type PasswordCredentialId struct { | ||
ObjectId string | ||
KeyId string | ||
} | ||
|
||
func (id PasswordCredentialId) String() string { | ||
return id.ObjectId + "/" + id.KeyId | ||
} | ||
|
||
func ParsePasswordCredentialId(id string) (PasswordCredentialId, error) { | ||
parts := strings.Split(id, "/") | ||
if len(parts) != 2 { | ||
return PasswordCredentialId{}, fmt.Errorf("Password Credential ID should be in the format {objectId}/{keyId} - but got %q", id) | ||
} | ||
|
||
if _, err := uuid.ParseUUID(parts[0]); err != nil { | ||
return PasswordCredentialId{}, fmt.Errorf("Object ID isn't a valid UUID (%q): %+v", id[0], err) | ||
} | ||
|
||
if _, err := uuid.ParseUUID(parts[1]); err != nil { | ||
return PasswordCredentialId{}, fmt.Errorf("Object ID isn't a valid UUID (%q): %+v", id[1], err) | ||
} | ||
|
||
return PasswordCredentialId{ | ||
ObjectId: parts[0], | ||
KeyId: parts[1], | ||
}, nil | ||
|
||
} | ||
|
||
func PasswordCredentialIdFrom(objectId, keyId string) PasswordCredentialId { | ||
return PasswordCredentialId{ | ||
ObjectId: objectId, | ||
KeyId: keyId, | ||
} | ||
} | ||
|
||
func PasswordCredentialForResource(d *schema.ResourceData) (*graphrbac.PasswordCredential, error) { | ||
value := d.Get("value").(string) | ||
|
||
// errors should be handled by the validation | ||
var keyId string | ||
if v, ok := d.GetOk("key_id"); ok { | ||
keyId = v.(string) | ||
} else { | ||
kid, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
keyId = kid | ||
} | ||
|
||
var endDate time.Time | ||
if v := d.Get("end_date").(string); v != "" { | ||
endDate, _ = time.Parse(time.RFC3339, v) | ||
} else if v := d.Get("end_date_relative").(string); v != "" { | ||
d, err := time.ParseDuration(v) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to parse `end_date_relative` (%s) as a duration", v) | ||
} | ||
endDate = time.Now().Add(d) | ||
} else { | ||
return nil, fmt.Errorf("one of `end_date` or `end_date_relative` must be specified") | ||
} | ||
|
||
credential := graphrbac.PasswordCredential{ | ||
KeyID: p.String(keyId), | ||
Value: p.String(value), | ||
EndDate: &date.Time{Time: endDate}, | ||
} | ||
|
||
if v, ok := d.GetOk("start_date"); ok { | ||
// errors will be handled by the validation | ||
startDate, _ := time.Parse(time.RFC3339, v.(string)) | ||
credential.StartDate = &date.Time{Time: startDate} | ||
} | ||
|
||
return &credential, nil | ||
} | ||
|
||
func PasswordCredentialResultFindByKeyId(creds graphrbac.PasswordCredentialListResult, keyId string) *graphrbac.PasswordCredential { | ||
var cred *graphrbac.PasswordCredential | ||
|
||
if creds.Value != nil { | ||
for _, c := range *creds.Value { | ||
if c.KeyID == nil { | ||
continue | ||
} | ||
|
||
if *c.KeyID == keyId { | ||
cred = &c | ||
break | ||
} | ||
} | ||
} | ||
|
||
return cred | ||
} | ||
|
||
func PasswordCredentialResultAdd(existing graphrbac.PasswordCredentialListResult, cred *graphrbac.PasswordCredential, errorOnDuplicate bool) (*[]graphrbac.PasswordCredential, error) { | ||
newCreds := make([]graphrbac.PasswordCredential, 0) | ||
|
||
if existing.Value != nil { | ||
if errorOnDuplicate { | ||
for _, v := range *existing.Value { | ||
if v.KeyID == nil { | ||
continue | ||
} | ||
|
||
if *v.KeyID == *cred.KeyID { | ||
return nil, fmt.Errorf("credential already exists found") | ||
} | ||
} | ||
} | ||
|
||
newCreds = *existing.Value | ||
} | ||
newCreds = append(newCreds, *cred) | ||
|
||
return &newCreds, nil | ||
} | ||
|
||
func PasswordCredentialResultRemoveByKeyId(existing graphrbac.PasswordCredentialListResult, keyId string) *[]graphrbac.PasswordCredential { | ||
newCreds := make([]graphrbac.PasswordCredential, 0) | ||
|
||
if existing.Value != nil { | ||
for _, v := range *existing.Value { | ||
if v.KeyID == nil { | ||
continue | ||
} | ||
|
||
if *v.KeyID == keyId { | ||
continue | ||
} | ||
|
||
newCreds = append(newCreds, v) | ||
} | ||
} | ||
|
||
return &newCreds | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package azuread | ||
|
||
// handle the case of using the same name for different kinds of resources | ||
func azureADLockByName(name string, resourceType string) { | ||
// handles the case of using the same name for different kinds of resources | ||
func azureADLockByName(resourceType string, name string) { | ||
armMutexKV.Lock(resourceType + "." + name) | ||
} | ||
|
||
func azureADUnlockByName(name string, resourceType string) { | ||
func azureADUnlockByName(resourceType string, name string) { | ||
armMutexKV.Unlock(resourceType + "." + name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.