Skip to content

Commit

Permalink
Add import
Browse files Browse the repository at this point in the history
  • Loading branch information
andriikushch committed Nov 15, 2024
1 parent 6ed2354 commit fac0780
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions internal/resources/cloudprovider/resource_azure_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cloudprovider

import (
"context"
"fmt"
"strings"

"github.com/grafana/terraform-provider-grafana/v3/internal/common"
"github.com/grafana/terraform-provider-grafana/v3/internal/common/cloudproviderapi"
Expand Down Expand Up @@ -109,6 +111,36 @@ func (r *resourceAzureCredential) Schema(ctx context.Context, req resource.Schem
}
}

func (r *resourceAzureCredential) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
parts := strings.SplitN(req.ID, ":", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
resp.Diagnostics.AddError("Invalid ID", fmt.Sprintf("Invalid ID: %s", req.ID))
return
}
stackID := parts[0]
resourceID := parts[1]

credentials, err := r.client.GetAzureCredential(
ctx,
stackID,
resourceID,
)
if err != nil {
resp.Diagnostics.AddError("Failed to get Azure credential", err.Error())
return
}

resp.State.Set(ctx, &resourceAzureCredentialModel{
ID: types.StringValue(req.ID),
Name: types.StringValue(credentials.Name),
TenantID: types.StringValue(credentials.TenantID),
ClientID: types.StringValue(credentials.ClientID),
StackID: types.StringValue(stackID),
ResourceID: types.StringValue(resourceID),
ClientSecret: types.StringValue(""), // We don't import the client secret
})
}

func (r *resourceAzureCredential) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data resourceAzureCredentialModel
diags := req.Plan.Get(ctx, &data)
Expand Down

0 comments on commit fac0780

Please sign in to comment.