Skip to content

Commit

Permalink
feat: allow looking up resource_group data source by name
Browse files Browse the repository at this point in the history
Useful when just grabbing the "default" resource group
  • Loading branch information
ligfx authored and gene-redpanda committed Sep 13, 2024
1 parent 1f4c474 commit 631d858
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
34 changes: 34 additions & 0 deletions redpanda/cloud/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,40 @@ func (cpCl *ControlPlaneClientSet) ResourceGroupForName(ctx context.Context, nam
return nil, fmt.Errorf("resource group %s not found", name)
}

// ResourceGroupForIDOrName gets the resource group for a given ID and/or name, or neither,
// and handles the error if the returned resource group is nil.
func (cpCl *ControlPlaneClientSet) ResourceGroupForIDOrName(ctx context.Context, id, name string) (*controlplanev1beta2.ResourceGroup, error) {
if id != "" {
rg, err := cpCl.ResourceGroupForID(ctx, id)
if err != nil {
return nil, err
}
if name != "" && rg.Name != name {
return nil, fmt.Errorf("unable to find resource group with id %q and name %q", id, name)
}
return rg, nil
}

if name != "" {
return cpCl.ResourceGroupForName(ctx, name)
}

request := &controlplanev1beta2.ListResourceGroupsRequest{}
listResp, err := cpCl.ResourceGroup.ListResourceGroups(ctx, request)
if listResp.ResourceGroups == nil {
err = fmt.Errorf("provider response was empty. Please report this issue to the provider developers")
}
if err != nil {
return nil, fmt.Errorf("unable to find resource groups: %w", err)
}
if len(listResp.ResourceGroups) > 1 {
return nil, fmt.Errorf("found more than one resource group matching filters")
} else if len(listResp.ResourceGroups) == 0 {
return nil, fmt.Errorf("unable to find any resource group matching filters")
}
return listResp.ResourceGroups[0], nil
}

// NetworkForID gets the Network for a given ID and handles the error if the
// returned network is nil.
func (cpCl *ControlPlaneClientSet) NetworkForID(ctx context.Context, id string) (*controlplanev1beta2.Network, error) {
Expand Down
7 changes: 5 additions & 2 deletions redpanda/resources/resourcegroup/data_resourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ func datasourceResourceGroupSchema() schema.Schema {
return schema.Schema{
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
Computed: true,
Optional: true,
Description: "UUID of the resource group",
},
"name": schema.StringAttribute{
Computed: true,
Optional: true,
Description: "Name of the resource group",
},
},
Expand All @@ -70,7 +72,8 @@ func datasourceResourceGroupSchema() schema.Schema {
func (n *DataSourceResourceGroup) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var model models.ResourceGroup
resp.Diagnostics.Append(req.Config.Get(ctx, &model)...)
rg, err := n.CpCl.ResourceGroupForID(ctx, model.ID.ValueString())

rg, err := n.CpCl.ResourceGroupForIDOrName(ctx, model.ID.ValueString(), model.Name.ValueString())
if err != nil {
resp.Diagnostics.AddError("failed to read resource group", err.Error())
return
Expand Down

0 comments on commit 631d858

Please sign in to comment.