-
Notifications
You must be signed in to change notification settings - Fork 643
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new datasource to get current user info
- Loading branch information
1 parent
a473122
commit 5bd82c5
Showing
11 changed files
with
192 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-data-source: cloudflare_user | ||
datasource/cloudflare_user: Create new datasource to get information about current user | ||
``` |
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,41 @@ | ||
--- | ||
page_title: "cloudflare_user Data Source - Cloudflare" | ||
subcategory: "" | ||
description: |- | ||
Use this data source to retrieve information about the currently authenticated user. | ||
--- | ||
|
||
# cloudflare_user (Data Source) | ||
|
||
Use this data source to retrieve information about the currently authenticated user. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "cloudflare_user" "me" {} | ||
data "cloudflare_api_token_permission_groups" "all" {} | ||
resource "cloudflare_api_token" "example" { | ||
name = "Terraform Cloud (Terraform)" | ||
policy { | ||
permission_groups = [ | ||
data.cloudflare_api_token_permission_groups.all.user["User Details Read"], | ||
] | ||
resources = { | ||
"com.cloudflare.api.user.${data.cloudflare_user.me.id}" = "*", | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `email` (String) The user's email address. | ||
- `id` (String) The user's unique identifier. | ||
- `username` (String) The user's username. | ||
|
||
|
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,15 @@ | ||
data "cloudflare_user" "me" {} | ||
data "cloudflare_api_token_permission_groups" "all" {} | ||
|
||
resource "cloudflare_api_token" "example" { | ||
name = "Terraform Cloud (Terraform)" | ||
policy { | ||
permission_groups = [ | ||
data.cloudflare_api_token_permission_groups.all.user["User Details Read"], | ||
|
||
] | ||
resources = { | ||
"com.cloudflare.api.user.${data.cloudflare_user.me.id}" = "*", | ||
} | ||
} | ||
} |
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,60 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/cloudflare/cloudflare-go" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ datasource.DataSource = &CloudflareUserDataSource{} | ||
|
||
func NewDataSource() datasource.DataSource { | ||
return &CloudflareUserDataSource{} | ||
} | ||
|
||
type CloudflareUserDataSource struct { | ||
client *cloudflare.API | ||
} | ||
|
||
func (r *CloudflareUserDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_user" | ||
} | ||
|
||
func (r *CloudflareUserDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*cloudflare.API) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"unexpected resource configure type", | ||
fmt.Sprintf("expected *cloudflare.API, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
r.client = client | ||
} | ||
|
||
func (r *CloudflareUserDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var data CloudflareUserDataSourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
user, err := r.client.UserDetails(ctx) | ||
if err != nil { | ||
resp.Diagnostics.AddError("unable to retrieve user details", err.Error()) | ||
return | ||
} | ||
data.ID = types.StringValue(user.ID) | ||
data.Email = types.StringValue(user.Email) | ||
data.Username = types.StringValue(user.Username) | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} |
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,32 @@ | ||
package user_test | ||
|
||
import ( | ||
"github.com/cloudflare/terraform-provider-cloudflare/internal/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestAccCloudflareUserDataSource(t *testing.T) { | ||
// Temporarily unset CLOUDFLARE_API_TOKEN if it is set as the API token | ||
// permission groups endpoint does not yet support the API tokens, and it | ||
// results in misleading state error messages. | ||
if os.Getenv("CLOUDFLARE_API_TOKEN") != "" { | ||
t.Setenv("CLOUDFLARE_API_TOKEN", "") | ||
} | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.TestAccPreCheck(t) }, | ||
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: `data "cloudflare_user" "test" {}`, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.cloudflare_user.test", "id"), | ||
resource.TestCheckResourceAttrSet("data.cloudflare_user.test", "email"), | ||
resource.TestCheckResourceAttrSet("data.cloudflare_user.test", "username"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,9 @@ | ||
package user | ||
|
||
import "github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
type CloudflareUserDataSourceModel struct { | ||
ID types.String `tfsdk:"id"` | ||
Email types.String `tfsdk:"email"` | ||
Username types.String `tfsdk:"username"` | ||
} |
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,27 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
) | ||
|
||
func (r *CloudflareUserDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Use this data source to retrieve information about the currently authenticated user.", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Description: "The user's unique identifier.", | ||
Computed: true, | ||
}, | ||
"email": schema.StringAttribute{ | ||
Description: "The user's email address.", | ||
Computed: true, | ||
}, | ||
"username": schema.StringAttribute{ | ||
Description: "The user's username.", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} |