-
Notifications
You must be signed in to change notification settings - Fork 630
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
Add cloudflare_account_member
resource
#78
Merged
patryk
merged 6 commits into
cloudflare:master
from
jacobbednarz:add-account-member-resource
Oct 8, 2018
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e298166
Add website documentation for `cloudflare_account_member`
jacobbednarz 2da08fc
Adds `cloudflare_account_member` resource
jacobbednarz 4252a08
Merge branch 'master' into add-account-member-resource
patryk 10419fd
Fix Cloudflare casing in function names
patryk a2fb28a
Swap to `OrganizationID` for lookups
jacobbednarz 620b8bb
s/memebers/members
jacobbednarz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,110 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
cloudflare "github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceCloudflareAccountMember() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCloudflareAccountMemberCreate, | ||
Read: resourceCloudflareAccountMemberRead, | ||
Update: resourceCloudflareAccountMemberUpdate, | ||
Delete: resourceCloudflareAccountMemberDelete, | ||
|
||
SchemaVersion: 0, | ||
Schema: map[string]*schema.Schema{ | ||
"email_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"role_ids": { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Decided to use IDs over Names here as the name could change but the IDs are pretty unique and won't trigger a bunch of extra updates. |
||
Type: schema.TypeList, | ||
Required: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCloudflareAccountMemberRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
|
||
_, err := client.AccountMember(client.OrganizationID, d.Id()) | ||
if err != nil { | ||
if strings.Contains(err.Error(), "Member not found") || | ||
strings.Contains(err.Error(), "HTTP status 404") { | ||
log.Printf("[WARN] Removing account member from state because it's not present in API") | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.SetId(d.Id()) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudflareAccountMemberDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
|
||
log.Printf("[INFO] Deleting Cloudflare account member ID: %s", d.Id()) | ||
|
||
err := client.DeleteAccountMember(client.OrganizationID, d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("error deleting Cloudflare account member: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceCloudflareAccountMemberCreate(d *schema.ResourceData, meta interface{}) error { | ||
memberEmailAddress := d.Get("email_address").(string) | ||
requestedMemberRoles := d.Get("role_ids").([]interface{}) | ||
|
||
client := meta.(*cloudflare.API) | ||
|
||
var accountMemberRoleIDs []string | ||
for _, roleID := range requestedMemberRoles { | ||
accountMemberRoleIDs = append(accountMemberRoleIDs, roleID.(string)) | ||
} | ||
|
||
r, err := client.CreateAccountMember(client.OrganizationID, memberEmailAddress, accountMemberRoleIDs) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating Cloudflare account member: %s", err) | ||
} | ||
|
||
if r.ID == "" { | ||
return fmt.Errorf("failed to find ID in create response; resource was empty") | ||
} | ||
|
||
d.SetId(r.ID) | ||
|
||
return resourceCloudflareAccountMemberRead(d, meta) | ||
} | ||
|
||
func resourceCloudflareAccountMemberUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*cloudflare.API) | ||
accountRoles := []cloudflare.AccountRole{} | ||
memberRoles := d.Get("role_ids").([]interface{}) | ||
|
||
for _, r := range memberRoles { | ||
accountRole, _ := client.AccountRole(client.OrganizationID, r.(string)) | ||
accountRoles = append(accountRoles, accountRole) | ||
} | ||
|
||
updatedAccountMember := cloudflare.AccountMember{Roles: accountRoles} | ||
_, err := client.UpdateAccountMember(client.OrganizationID, d.Id(), updatedAccountMember) | ||
if err != nil { | ||
return fmt.Errorf("failed to update Cloudflare account member: %s", err) | ||
} | ||
|
||
return resourceCloudflareAccountMemberRead(d, meta) | ||
} |
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,30 @@ | ||
--- | ||
layout: "cloudflare" | ||
page_title: "Cloudflare: cloudflare_account_member" | ||
sidebar_current: "docs-cloudflare-resource-account-member" | ||
description: |- | ||
Provides a resource which manages Cloudflare account members. | ||
--- | ||
|
||
# cloudflare_account_member | ||
|
||
Provides a resource which manages Cloudflare account members. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "cloudflare_account_member" "example_user" { | ||
email_address = "[email protected]" | ||
role_ids = [ | ||
"68b329da9893e34099c7d8ad5cb9c940", | ||
"d784fa8b6d98d27699781bd9a7cf19f0" | ||
] | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `email_address` - (Required) The email address of the user who you wish to manage. Note: Following creation, this field becomes read only via the API and cannot be updated. | ||
* `role_ids` - (Required) Array of account role IDs that you want to assign to a member. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll throw up a PR soon that will expand on the "how to develop the provider" section since I've had a few people ask me and the documentation is lacking.