diff --git a/files.gen b/files.gen index e261550..a11a94d 100755 --- a/files.gen +++ b/files.gen @@ -16,7 +16,6 @@ internal/provider/type_request.go internal/provider/type_reviewers.go internal/provider/type_step.go internal/provider/type_grant_workflow.go -examples/README.md go.mod go.sum internal/planmodifiers/boolplanmodifier/suppress_diff.go @@ -145,13 +144,13 @@ internal/sdk/pkg/models/shared/reviewupdateparams.go internal/sdk/pkg/models/shared/security.go USAGE.md internal/provider/provider.go -examples/provider/provider.tf internal/provider/demo_resource.go internal/provider/demo_resource_sdk.go -examples/resources/abbey_demo/resource.tf internal/provider/grantkit_resource.go internal/provider/grantkit_resource_sdk.go -examples/resources/abbey_grant_kit/resource.tf internal/provider/identity_resource.go internal/provider/identity_resource_sdk.go -examples/resources/abbey_identity/resource.tf \ No newline at end of file +internal/provider/grantkit_data_source.go +internal/provider/grantkit_data_source_sdk.go +internal/provider/identity_data_source.go +internal/provider/identity_data_source_sdk.go diff --git a/internal/provider/grantkit_data_source.go b/internal/provider/grantkit_data_source.go new file mode 100755 index 0000000..ad38499 --- /dev/null +++ b/internal/provider/grantkit_data_source.go @@ -0,0 +1,408 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package provider + +import ( + "abbey/v2/internal/sdk" + "abbey/v2/internal/sdk/pkg/models/operations" + "context" + "fmt" + + "abbey/v2/internal/validators" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" +) + +// Ensure provider defined types fully satisfy framework interfaces. +var _ datasource.DataSource = &GrantKitDataSource{} +var _ datasource.DataSourceWithConfigure = &GrantKitDataSource{} + +func NewGrantKitDataSource() datasource.DataSource { + return &GrantKitDataSource{} +} + +// GrantKitDataSource is the data source implementation. +type GrantKitDataSource struct { + client *sdk.SDK +} + +// GrantKitDataSourceModel describes the data model. +type GrantKitDataSourceModel struct { + CreatedAt types.String `tfsdk:"created_at"` + CurrentVersionID types.String `tfsdk:"current_version_id"` + Description types.String `tfsdk:"description"` + Grants []Grant `tfsdk:"grants"` + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` + Output Output `tfsdk:"output"` + Policies []Policy `tfsdk:"policies"` + Requests []Request `tfsdk:"requests"` + UpdatedAt types.String `tfsdk:"updated_at"` + Workflow *GrantWorkflow `tfsdk:"workflow"` +} + +// Metadata returns the data source type name. +func (r *GrantKitDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_grant_kit" +} + +// Schema defines the schema for the data source. +func (r *GrantKitDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "GrantKit DataSource", + + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "current_version_id": schema.StringAttribute{ + Computed: true, + }, + "description": schema.StringAttribute{ + Computed: true, + }, + "grants": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "deleted": schema.BoolAttribute{ + Computed: true, + }, + "grant_kit_id": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_version_id": schema.StringAttribute{ + Computed: true, + }, + "id": schema.StringAttribute{ + Computed: true, + }, + "organization_id": schema.StringAttribute{ + Computed: true, + }, + "request_id": schema.StringAttribute{ + Computed: true, + }, + "updated_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "user_id": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + "id": schema.StringAttribute{ + Required: true, + Description: `The ID of the grant kit or resource to retrieve.`, + }, + "name": schema.StringAttribute{ + Computed: true, + }, + "output": schema.SingleNestedAttribute{ + Computed: true, + Attributes: map[string]schema.Attribute{ + "append": schema.StringAttribute{ + Computed: true, + }, + "location": schema.StringAttribute{ + Computed: true, + }, + "overwrite": schema.StringAttribute{ + Computed: true, + }, + }, + }, + "policies": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "bundle": schema.StringAttribute{ + Computed: true, + }, + "query": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + "requests": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "grant_id": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_id": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_name": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_version_id": schema.StringAttribute{ + Computed: true, + }, + "id": schema.StringAttribute{ + Computed: true, + }, + "pull_request": schema.StringAttribute{ + Computed: true, + }, + "reason": schema.StringAttribute{ + Computed: true, + }, + "reviews": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "grant": schema.SingleNestedAttribute{ + Computed: true, + Attributes: map[string]schema.Attribute{ + "created_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "deleted": schema.BoolAttribute{ + Computed: true, + }, + "grant_kit_id": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_version_id": schema.StringAttribute{ + Computed: true, + }, + "id": schema.StringAttribute{ + Computed: true, + }, + "organization_id": schema.StringAttribute{ + Computed: true, + }, + "request_id": schema.StringAttribute{ + Computed: true, + }, + "updated_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "user_id": schema.StringAttribute{ + Computed: true, + }, + }, + Description: `Success`, + }, + "grant_id": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_name": schema.StringAttribute{ + Computed: true, + }, + "grant_kit_version_id": schema.StringAttribute{ + Computed: true, + }, + "id": schema.StringAttribute{ + Computed: true, + }, + "pull_request": schema.StringAttribute{ + Computed: true, + }, + "reason": schema.StringAttribute{ + Computed: true, + }, + "request_id": schema.StringAttribute{ + Computed: true, + }, + "status": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + stringvalidator.OneOf( + "Pending", + "Denied", + "Approved", + "Canceled", + ), + }, + Description: `must be one of [Pending, Denied, Approved, Canceled]`, + }, + "updated_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "user_email": schema.StringAttribute{ + Computed: true, + }, + "user_id": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + "status": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + stringvalidator.OneOf( + "Pending", + "Denied", + "Approved", + "Canceled", + ), + }, + Description: `must be one of [Pending, Denied, Approved, Canceled]`, + }, + "updated_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "user_id": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + "updated_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "workflow": schema.SingleNestedAttribute{ + Computed: true, + Attributes: map[string]schema.Attribute{ + "steps": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "reviewers": schema.SingleNestedAttribute{ + Computed: true, + Attributes: map[string]schema.Attribute{ + "all_of": schema.ListAttribute{ + Computed: true, + ElementType: types.StringType, + }, + "one_of": schema.ListAttribute{ + Computed: true, + ElementType: types.StringType, + }, + }, + }, + "skip_if": schema.ListNestedAttribute{ + Computed: true, + NestedObject: schema.NestedAttributeObject{ + Attributes: map[string]schema.Attribute{ + "bundle": schema.StringAttribute{ + Computed: true, + }, + "query": schema.StringAttribute{ + Computed: true, + }, + }, + }, + }, + }, + }, + }, + }, + }, + }, + } +} + +func (r *GrantKitDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + // Prevent panic if the provider has not been configured. + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(*sdk.SDK) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected DataSource Configure Type", + fmt.Sprintf("Expected *sdk.SDK, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + + return + } + + r.client = client +} + +func (r *GrantKitDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data *GrantKitDataSourceModel + var item types.Object + + resp.Diagnostics.Append(req.Config.Get(ctx, &item)...) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(item.As(ctx, &data, basetypes.ObjectAsOptions{ + UnhandledNullAsEmpty: true, + UnhandledUnknownAsEmpty: true, + })...) + + if resp.Diagnostics.HasError() { + return + } + + grantKitIDOrName := data.ID.ValueString() + request := operations.GetGrantKitByIDRequest{ + GrantKitIDOrName: grantKitIDOrName, + } + res, err := r.client.GrantKits.GetGrantKitByID(ctx, request) + if err != nil { + resp.Diagnostics.AddError("failure to invoke API", err.Error()) + return + } + if res == nil { + resp.Diagnostics.AddError("unexpected response from API", fmt.Sprintf("%v", res)) + return + } + if res.StatusCode != 200 { + resp.Diagnostics.AddError(fmt.Sprintf("unexpected response from API. Got an unexpected response code %v", res.StatusCode), debugResponse(res.RawResponse)) + return + } + if res.GrantKit == nil { + resp.Diagnostics.AddError("unexpected response from API. No response body", debugResponse(res.RawResponse)) + return + } + data.RefreshFromGetResponse(res.GrantKit) + + // Save updated data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/provider/grantkit_data_source_sdk.go b/internal/provider/grantkit_data_source_sdk.go new file mode 100755 index 0000000..2fe38bb --- /dev/null +++ b/internal/provider/grantkit_data_source_sdk.go @@ -0,0 +1,160 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package provider + +import ( + "abbey/v2/internal/sdk/pkg/models/shared" + "github.com/hashicorp/terraform-plugin-framework/types" + "time" +) + +func (r *GrantKitDataSourceModel) RefreshFromGetResponse(resp *shared.GrantKit) { + r.CreatedAt = types.StringValue(resp.CreatedAt.Format(time.RFC3339)) + r.CurrentVersionID = types.StringValue(resp.CurrentVersionID) + r.Description = types.StringValue(resp.Description) + r.Grants = nil + for _, grantsItem := range resp.Grants { + var grants1 Grant + grants1.CreatedAt = types.StringValue(grantsItem.CreatedAt.Format(time.RFC3339)) + grants1.Deleted = types.BoolValue(grantsItem.Deleted) + grants1.GrantKitID = types.StringValue(grantsItem.GrantKitID) + grants1.GrantKitVersionID = types.StringValue(grantsItem.GrantKitVersionID) + grants1.ID = types.StringValue(grantsItem.ID) + grants1.OrganizationID = types.StringValue(grantsItem.OrganizationID) + grants1.RequestID = types.StringValue(grantsItem.RequestID) + grants1.UpdatedAt = types.StringValue(grantsItem.UpdatedAt.Format(time.RFC3339)) + grants1.UserID = types.StringValue(grantsItem.UserID) + r.Grants = append(r.Grants, grants1) + } + r.ID = types.StringValue(resp.ID) + r.Name = types.StringValue(resp.Name) + if resp.Output.Append != nil { + r.Output.Append = types.StringValue(*resp.Output.Append) + } else { + r.Output.Append = types.StringNull() + } + r.Output.Location = types.StringValue(resp.Output.Location) + if resp.Output.Overwrite != nil { + r.Output.Overwrite = types.StringValue(*resp.Output.Overwrite) + } else { + r.Output.Overwrite = types.StringNull() + } + r.Policies = nil + for _, policiesItem := range resp.Policies { + var policies1 Policy + if policiesItem.Bundle != nil { + policies1.Bundle = types.StringValue(*policiesItem.Bundle) + } else { + policies1.Bundle = types.StringNull() + } + if policiesItem.Query != nil { + policies1.Query = types.StringValue(*policiesItem.Query) + } else { + policies1.Query = types.StringNull() + } + r.Policies = append(r.Policies, policies1) + } + r.Requests = nil + for _, requestsItem := range resp.Requests { + var requests1 Request + requests1.CreatedAt = types.StringValue(requestsItem.CreatedAt.Format(time.RFC3339)) + requests1.GrantID = types.StringValue(requestsItem.GrantID) + requests1.GrantKitID = types.StringValue(requestsItem.GrantKitID) + if requestsItem.GrantKitName != nil { + requests1.GrantKitName = types.StringValue(*requestsItem.GrantKitName) + } else { + requests1.GrantKitName = types.StringNull() + } + requests1.GrantKitVersionID = types.StringValue(requestsItem.GrantKitVersionID) + requests1.ID = types.StringValue(requestsItem.ID) + requests1.PullRequest = types.StringValue(requestsItem.PullRequest) + requests1.Reason = types.StringValue(requestsItem.Reason) + requests1.Reviews = nil + for _, reviewsItem := range requestsItem.Reviews { + var reviews1 Review + reviews1.CreatedAt = types.StringValue(reviewsItem.CreatedAt.Format(time.RFC3339)) + if reviews1.Grant == nil { + reviews1.Grant = &Grant{} + } + if reviewsItem.Grant == nil { + reviews1.Grant = nil + } else { + reviews1.Grant = &Grant{} + reviews1.Grant.CreatedAt = types.StringValue(reviewsItem.Grant.CreatedAt.Format(time.RFC3339)) + reviews1.Grant.Deleted = types.BoolValue(reviewsItem.Grant.Deleted) + reviews1.Grant.GrantKitID = types.StringValue(reviewsItem.Grant.GrantKitID) + reviews1.Grant.GrantKitVersionID = types.StringValue(reviewsItem.Grant.GrantKitVersionID) + reviews1.Grant.ID = types.StringValue(reviewsItem.Grant.ID) + reviews1.Grant.OrganizationID = types.StringValue(reviewsItem.Grant.OrganizationID) + reviews1.Grant.RequestID = types.StringValue(reviewsItem.Grant.RequestID) + reviews1.Grant.UpdatedAt = types.StringValue(reviewsItem.Grant.UpdatedAt.Format(time.RFC3339)) + reviews1.Grant.UserID = types.StringValue(reviewsItem.Grant.UserID) + } + reviews1.GrantID = types.StringValue(reviewsItem.GrantID) + reviews1.GrantKitName = types.StringValue(reviewsItem.GrantKitName) + reviews1.GrantKitVersionID = types.StringValue(reviewsItem.GrantKitVersionID) + reviews1.ID = types.StringValue(reviewsItem.ID) + reviews1.PullRequest = types.StringValue(reviewsItem.PullRequest) + reviews1.Reason = types.StringValue(reviewsItem.Reason) + reviews1.RequestID = types.StringValue(reviewsItem.RequestID) + reviews1.Status = types.StringValue(string(reviewsItem.Status)) + reviews1.UpdatedAt = types.StringValue(reviewsItem.UpdatedAt.Format(time.RFC3339)) + if reviewsItem.UserEmail != nil { + reviews1.UserEmail = types.StringValue(*reviewsItem.UserEmail) + } else { + reviews1.UserEmail = types.StringNull() + } + reviews1.UserID = types.StringValue(reviewsItem.UserID) + requests1.Reviews = append(requests1.Reviews, reviews1) + } + requests1.Status = types.StringValue(string(requestsItem.Status)) + requests1.UpdatedAt = types.StringValue(requestsItem.UpdatedAt.Format(time.RFC3339)) + requests1.UserID = types.StringValue(requestsItem.UserID) + r.Requests = append(r.Requests, requests1) + } + r.UpdatedAt = types.StringValue(resp.UpdatedAt.Format(time.RFC3339)) + if r.Workflow == nil { + r.Workflow = &GrantWorkflow{} + } + if resp.Workflow == nil { + r.Workflow = nil + } else { + r.Workflow = &GrantWorkflow{} + r.Workflow.Steps = nil + for _, stepsItem := range resp.Workflow.Steps { + var steps1 Step + if steps1.Reviewers == nil { + steps1.Reviewers = &Reviewers{} + } + if stepsItem.Reviewers == nil { + steps1.Reviewers = nil + } else { + steps1.Reviewers = &Reviewers{} + steps1.Reviewers.AllOf = nil + for _, v := range stepsItem.Reviewers.AllOf { + steps1.Reviewers.AllOf = append(steps1.Reviewers.AllOf, types.StringValue(v)) + } + steps1.Reviewers.OneOf = nil + for _, v := range stepsItem.Reviewers.OneOf { + steps1.Reviewers.OneOf = append(steps1.Reviewers.OneOf, types.StringValue(v)) + } + } + steps1.SkipIf = nil + for _, skipIfItem := range stepsItem.SkipIf { + var skipIf1 Policy + if skipIfItem.Bundle != nil { + skipIf1.Bundle = types.StringValue(*skipIfItem.Bundle) + } else { + skipIf1.Bundle = types.StringNull() + } + if skipIfItem.Query != nil { + skipIf1.Query = types.StringValue(*skipIfItem.Query) + } else { + skipIf1.Query = types.StringNull() + } + steps1.SkipIf = append(steps1.SkipIf, skipIf1) + } + r.Workflow.Steps = append(r.Workflow.Steps, steps1) + } + } +} diff --git a/internal/provider/grantkit_resource.go b/internal/provider/grantkit_resource.go index aa029af..7aed9a7 100755 --- a/internal/provider/grantkit_resource.go +++ b/internal/provider/grantkit_resource.go @@ -130,7 +130,6 @@ func (r *GrantKitResource) Schema(ctx context.Context, req resource.SchemaReques }, }, "policies": schema.ListNestedAttribute{ - Computed: true, Optional: true, NestedObject: schema.NestedAttributeObject{ Attributes: map[string]schema.Attribute{ @@ -304,7 +303,6 @@ func (r *GrantKitResource) Schema(ctx context.Context, req resource.SchemaReques }, }, "workflow": schema.SingleNestedAttribute{ - Computed: true, Optional: true, Attributes: map[string]schema.Attribute{ "steps": schema.ListNestedAttribute{ diff --git a/internal/provider/grantkit_resource_sdk.go b/internal/provider/grantkit_resource_sdk.go index 6e1428f..3dd411b 100755 --- a/internal/provider/grantkit_resource_sdk.go +++ b/internal/provider/grantkit_resource_sdk.go @@ -29,7 +29,7 @@ func (r *GrantKitResourceModel) ToCreateSDKType() *shared.GrantKitCreateParams { Location: location, Overwrite: overwrite, } - policies := make([]shared.Policy, 0) + var policies []shared.Policy = nil for _, policiesItem := range r.Policies { bundle := new(string) if !policiesItem.Bundle.IsUnknown() && !policiesItem.Bundle.IsNull() { @@ -50,15 +50,15 @@ func (r *GrantKitResourceModel) ToCreateSDKType() *shared.GrantKitCreateParams { } var workflow *shared.GrantWorkflow if r.Workflow != nil { - steps := make([]shared.Step, 0) + var steps []shared.Step = nil for _, stepsItem := range r.Workflow.Steps { var reviewers *shared.Reviewers if stepsItem.Reviewers != nil { - allOf := make([]string, 0) + var allOf []string = nil for _, allOfItem := range stepsItem.Reviewers.AllOf { allOf = append(allOf, allOfItem.ValueString()) } - oneOf := make([]string, 0) + var oneOf []string = nil for _, oneOfItem := range stepsItem.Reviewers.OneOf { oneOf = append(oneOf, oneOfItem.ValueString()) } @@ -67,7 +67,7 @@ func (r *GrantKitResourceModel) ToCreateSDKType() *shared.GrantKitCreateParams { OneOf: oneOf, } } - skipIf := make([]shared.Policy, 0) + var skipIf []shared.Policy = nil for _, skipIfItem := range stepsItem.SkipIf { bundle1 := new(string) if !skipIfItem.Bundle.IsUnknown() && !skipIfItem.Bundle.IsNull() { @@ -131,7 +131,7 @@ func (r *GrantKitResourceModel) ToUpdateSDKType() *shared.GrantKitUpdateParams { Location: location, Overwrite: overwrite, } - policies := make([]shared.Policy, 0) + var policies []shared.Policy = nil for _, policiesItem := range r.Policies { bundle := new(string) if !policiesItem.Bundle.IsUnknown() && !policiesItem.Bundle.IsNull() { @@ -152,15 +152,15 @@ func (r *GrantKitResourceModel) ToUpdateSDKType() *shared.GrantKitUpdateParams { } var workflow *shared.GrantWorkflow if r.Workflow != nil { - steps := make([]shared.Step, 0) + var steps []shared.Step = nil for _, stepsItem := range r.Workflow.Steps { var reviewers *shared.Reviewers if stepsItem.Reviewers != nil { - allOf := make([]string, 0) + var allOf []string = nil for _, allOfItem := range stepsItem.Reviewers.AllOf { allOf = append(allOf, allOfItem.ValueString()) } - oneOf := make([]string, 0) + var oneOf []string = nil for _, oneOfItem := range stepsItem.Reviewers.OneOf { oneOf = append(oneOf, oneOfItem.ValueString()) } @@ -169,7 +169,7 @@ func (r *GrantKitResourceModel) ToUpdateSDKType() *shared.GrantKitUpdateParams { OneOf: oneOf, } } - skipIf := make([]shared.Policy, 0) + var skipIf []shared.Policy = nil for _, skipIfItem := range stepsItem.SkipIf { bundle1 := new(string) if !skipIfItem.Bundle.IsUnknown() && !skipIfItem.Bundle.IsNull() { diff --git a/internal/provider/identity_data_source.go b/internal/provider/identity_data_source.go new file mode 100755 index 0000000..38736ed --- /dev/null +++ b/internal/provider/identity_data_source.go @@ -0,0 +1,146 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package provider + +import ( + "abbey/v2/internal/sdk" + "abbey/v2/internal/sdk/pkg/models/operations" + "context" + "fmt" + + "abbey/v2/internal/validators" + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" +) + +// Ensure provider defined types fully satisfy framework interfaces. +var _ datasource.DataSource = &IdentityDataSource{} +var _ datasource.DataSourceWithConfigure = &IdentityDataSource{} + +func NewIdentityDataSource() datasource.DataSource { + return &IdentityDataSource{} +} + +// IdentityDataSource is the data source implementation. +type IdentityDataSource struct { + client *sdk.SDK +} + +// IdentityDataSourceModel describes the data model. +type IdentityDataSourceModel struct { + AbbeyAccount types.String `tfsdk:"abbey_account"` + CreatedAt types.String `tfsdk:"created_at"` + ID types.String `tfsdk:"id"` + Metadata types.String `tfsdk:"metadata"` + Source types.String `tfsdk:"source"` + UpdatedAt types.String `tfsdk:"updated_at"` +} + +// Metadata returns the data source type name. +func (r *IdentityDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { + resp.TypeName = req.ProviderTypeName + "_identity" +} + +// Schema defines the schema for the data source. +func (r *IdentityDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { + resp.Schema = schema.Schema{ + MarkdownDescription: "Identity DataSource", + + Attributes: map[string]schema.Attribute{ + "abbey_account": schema.StringAttribute{ + Computed: true, + }, + "created_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + "id": schema.StringAttribute{ + Required: true, + Description: `The ID of the identity to retrieve`, + }, + "metadata": schema.StringAttribute{ + Computed: true, + Description: `Json encoded string. See documentation for details.`, + }, + "source": schema.StringAttribute{ + Computed: true, + }, + "updated_at": schema.StringAttribute{ + Computed: true, + Validators: []validator.String{ + validators.IsRFC3339(), + }, + }, + }, + } +} + +func (r *IdentityDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + // Prevent panic if the provider has not been configured. + if req.ProviderData == nil { + return + } + + client, ok := req.ProviderData.(*sdk.SDK) + + if !ok { + resp.Diagnostics.AddError( + "Unexpected DataSource Configure Type", + fmt.Sprintf("Expected *sdk.SDK, got: %T. Please report this issue to the provider developers.", req.ProviderData), + ) + + return + } + + r.client = client +} + +func (r *IdentityDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { + var data *IdentityDataSourceModel + var item types.Object + + resp.Diagnostics.Append(req.Config.Get(ctx, &item)...) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(item.As(ctx, &data, basetypes.ObjectAsOptions{ + UnhandledNullAsEmpty: true, + UnhandledUnknownAsEmpty: true, + })...) + + if resp.Diagnostics.HasError() { + return + } + + identityID := data.ID.ValueString() + request := operations.GetIdentityRequest{ + IdentityID: identityID, + } + res, err := r.client.Identities.GetIdentity(ctx, request) + if err != nil { + resp.Diagnostics.AddError("failure to invoke API", err.Error()) + return + } + if res == nil { + resp.Diagnostics.AddError("unexpected response from API", fmt.Sprintf("%v", res)) + return + } + if res.StatusCode != 200 { + resp.Diagnostics.AddError(fmt.Sprintf("unexpected response from API. Got an unexpected response code %v", res.StatusCode), debugResponse(res.RawResponse)) + return + } + if res.Identity == nil { + resp.Diagnostics.AddError("unexpected response from API. No response body", debugResponse(res.RawResponse)) + return + } + data.RefreshFromGetResponse(res.Identity) + + // Save updated data into Terraform state + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) +} diff --git a/internal/provider/identity_data_source_sdk.go b/internal/provider/identity_data_source_sdk.go new file mode 100755 index 0000000..ed453f4 --- /dev/null +++ b/internal/provider/identity_data_source_sdk.go @@ -0,0 +1,18 @@ +// Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT. + +package provider + +import ( + "abbey/v2/internal/sdk/pkg/models/shared" + "github.com/hashicorp/terraform-plugin-framework/types" + "time" +) + +func (r *IdentityDataSourceModel) RefreshFromGetResponse(resp *shared.Identity) { + r.AbbeyAccount = types.StringValue(resp.AbbeyAccount) + r.CreatedAt = types.StringValue(resp.CreatedAt.Format(time.RFC3339)) + r.ID = types.StringValue(resp.ID) + r.Metadata = types.StringValue(resp.Metadata) + r.Source = types.StringValue(resp.Source) + r.UpdatedAt = types.StringValue(resp.UpdatedAt.Format(time.RFC3339)) +} diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 1d9fd5f..c9a79ee 100755 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -92,7 +92,10 @@ func (p *AbbeyProvider) Resources(ctx context.Context) []func() resource.Resourc } func (p *AbbeyProvider) DataSources(ctx context.Context) []func() datasource.DataSource { - return []func() datasource.DataSource{} + return []func() datasource.DataSource{ + NewGrantKitDataSource, + NewIdentityDataSource, + } } func New(version string) func() provider.Provider { diff --git a/internal/sdk/demo.go b/internal/sdk/demo.go index b0d148e..ac0415d 100755 --- a/internal/sdk/demo.go +++ b/internal/sdk/demo.go @@ -186,26 +186,20 @@ func (s *demo) DeleteDemo(ctx context.Context, request shared.DemoParams) (*oper // GetDemo - Get Demo // Get the demo response -func (s *demo) GetDemo(ctx context.Context, request shared.DemoParams) (*operations.GetDemoResponse, error) { +func (s *demo) GetDemo(ctx context.Context, request operations.GetDemoRequest) (*operations.GetDemoResponse, error) { baseURL := utils.ReplaceParameters(s.sdkConfiguration.GetServerDetails()) url := strings.TrimSuffix(baseURL, "/") + "/demo" - bodyReader, reqContentType, err := utils.SerializeRequestBody(ctx, request, "Request", "json") - if err != nil { - return nil, fmt.Errorf("error serializing request body: %w", err) - } - if bodyReader == nil { - return nil, fmt.Errorf("request body is required") - } - - req, err := http.NewRequestWithContext(ctx, "GET", url, bodyReader) + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) if err != nil { return nil, fmt.Errorf("error creating request: %w", err) } req.Header.Set("Accept", "application/json") req.Header.Set("user-agent", fmt.Sprintf("speakeasy-sdk/%s %s %s %s", s.sdkConfiguration.Language, s.sdkConfiguration.SDKVersion, s.sdkConfiguration.GenVersion, s.sdkConfiguration.OpenAPIDocVersion)) - req.Header.Set("Content-Type", reqContentType) + if err := utils.PopulateQueryParams(ctx, req, request, nil); err != nil { + return nil, fmt.Errorf("error populating query params: %w", err) + } client := s.sdkConfiguration.SecurityClient diff --git a/internal/sdk/pkg/models/operations/getdemo.go b/internal/sdk/pkg/models/operations/getdemo.go index 4146d67..34b8f68 100755 --- a/internal/sdk/pkg/models/operations/getdemo.go +++ b/internal/sdk/pkg/models/operations/getdemo.go @@ -7,6 +7,11 @@ import ( "net/http" ) +type GetDemoRequest struct { + // The email of the user + Email string `queryParam:"style=form,explode=true,name=email"` +} + type GetDemoResponse struct { ContentType string // Success diff --git a/internal/sdk/sdk.go b/internal/sdk/sdk.go index 3d4e673..2ef3668 100755 --- a/internal/sdk/sdk.go +++ b/internal/sdk/sdk.go @@ -178,7 +178,7 @@ func New(opts ...SDKOption) *SDK { Language: "terraform", OpenAPIDocVersion: "v1", SDKVersion: "2.2.1", - GenVersion: "2.75.1", + GenVersion: "2.81.1", }, } for _, opt := range opts { diff --git a/internal/validators/ExactlyOneChild.go b/internal/validators/ExactlyOneChild.go index 839ef01..5741558 100755 --- a/internal/validators/ExactlyOneChild.go +++ b/internal/validators/ExactlyOneChild.go @@ -15,6 +15,10 @@ type exactlyOneChild struct { } func (validator exactlyOneChild) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) { + // Only validate the attribute configuration value if it is known. + if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { + return + } defined := make(map[string]bool) count := 0 for key, attr := range req.ConfigValue.Attributes() {