diff --git a/redpanda/cloud/controlplane.go b/redpanda/cloud/controlplane.go index b57cb48d..cd25b272 100644 --- a/redpanda/cloud/controlplane.go +++ b/redpanda/cloud/controlplane.go @@ -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) { diff --git a/redpanda/resources/resourcegroup/data_resourcegroup.go b/redpanda/resources/resourcegroup/data_resourcegroup.go index 6aeca83a..501efdc4 100644 --- a/redpanda/resources/resourcegroup/data_resourcegroup.go +++ b/redpanda/resources/resourcegroup/data_resourcegroup.go @@ -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", }, }, @@ -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