Skip to content
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

WIP: Add cloudflare_access_group data source #3072

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions internal/sdkv2provider/data_source_access_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package sdkv2provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"

cloudflare "github.com/cloudflare/cloudflare-go"
"github.com/cloudflare/terraform-provider-cloudflare/internal/consts"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceCloudflareAccessGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudflareAccessGroupRead,
Schema: map[string]*schema.Schema{
consts.AccountIDSchemaKey: {
Description: consts.AccountIDSchemaDescription,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{consts.ZoneIDSchemaKey},
},
consts.ZoneIDSchemaKey: {
Description: consts.ZoneIDSchemaDescription,
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{consts.AccountIDSchemaKey},
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceCloudflareAccessGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*cloudflare.API)
accountID := d.Get(consts.AccountIDSchemaKey).(string)
d.SetId(accountID)
searchAccessGroupName := d.Get("name").(string)

tflog.Debug(ctx, "reading accounts")
identifier, err := initIdentifier(d)
if err != nil {
return diag.FromErr(err)
}

// Call the Cloudflare API to retrieve the access group by name
accessGroups, _, err := client.ListAccessGroups(ctx, identifier, cloudflare.ListAccessGroupsParams{})
if err != nil {
return diag.FromErr(fmt.Errorf("error listing Access Groups: %w", err))
}
if len(accessGroups) == 0 {
return diag.Errorf("no Access groups found")
}
var accessGroup cloudflare.AccessGroup
for _, group := range accessGroups {
if group.Name == searchAccessGroupName {
accessGroup = group
break
}
}
if accessGroup.ID == "" {
return diag.Errorf("No Access groups matching name %q", searchAccessGroupName)
}
d.SetId(accessGroup.ID)
d.Set("name", accessGroup.Name)

return nil
}
3 changes: 2 additions & 1 deletion internal/sdkv2provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const (
MAXIMUM_NUMBER_OF_ENTITIES_REACHED_SUMMARY = "You've attempted to add a new %[1]s to the `terraform-plugin-sdkv2` which is no longer considered suitable for use."
MAXIMUM_NUMBER_OF_ENTITIES_REACHED_DETAIL = "Due the number of known internal issues with `terraform-plugin-sdkv2` (most notably handling of zero values), we are no longer recommending using it and instead, advise using `terraform-plugin-framework` exclusively. If you must use terraform-plugin-sdkv2 for this new %[1]s you should first discuss it with a maintainer to fully understand the impact and potential ramifications. Only then should you bump %[2]s to include your %[1]s."
MAXIMUM_ALLOWED_SDKV2_RESOURCES = 108
MAXIMUM_ALLOWED_SDKV2_DATASOURCES = 19
MAXIMUM_ALLOWED_SDKV2_DATASOURCES = 20
)

func init() {
Expand Down Expand Up @@ -185,6 +185,7 @@ func New(version string) func() *schema.Provider {
"cloudflare_zone_dnssec": dataSourceCloudflareZoneDNSSEC(),
"cloudflare_zone": dataSourceCloudflareZone(),
"cloudflare_zones": dataSourceCloudflareZones(),
"cloudflare_access_group": dataSourceCloudflareAccessGroup(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
Loading