func()

in internal/provider/apikey_resource.go [108:150]


func (r *apiKeyResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	// Retrieve values from plan
	var plan apiKeyResourceModel
	diags := req.Plan.Get(ctx, &plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	// Generate API request body from plan
	var apiKeyCreate = client.ApiKeyCreate{
		AllowedPath: plan.AllowedPath.ValueString(),
		ExpiredAt:   plan.ExpiredAt.ValueString(),
		Name:        plan.Name.ValueString(),
		Type:        plan.Type.ValueString(),
	}

	// Create new apikey
	apiKey, err := r.client.CreateApiKey(apiKeyCreate)
	if err != nil {
		resp.Diagnostics.AddError(
			"Error creating apiKey",
			"Could not create apiKey, unexpected error: "+err.Error(),
		)
		return
	}

	// Map response body to schema and populate Computed attribute values
	plan.ID = types.StringValue(strconv.Itoa(apiKey.ID))
	plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))
	plan.AllowedPath = types.StringValue(apiKey.AllowedPath)
	plan.ApiKey = types.StringValue(apiKey.ApiKey)
	plan.ExpiredAt = types.StringValue(apiKey.ExpiredAt)
	plan.Name = types.StringValue(apiKey.Name)
	plan.Type = types.StringValue(apiKey.Type)

	// Set state to fully populated data
	diags = resp.State.Set(ctx, plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}
}