Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
vandyliu committed Apr 4, 2024
1 parent 1880e2f commit b5b8da0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 84 deletions.
10 changes: 2 additions & 8 deletions docs/data-sources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,24 @@ data "astronomer_workspace" "example" {
<a id="nestedatt--created_by"></a>
### Nested Schema for `created_by`

Required:

- `id` (String)

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)


<a id="nestedatt--updated_by"></a>
### Nested Schema for `updated_by`

Required:

- `id` (String)

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)
10 changes: 2 additions & 8 deletions docs/resources/workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,24 @@ resource "workspace_resource" "example" {
<a id="nestedatt--created_by"></a>
### Nested Schema for `created_by`

Required:

- `id` (String)

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)


<a id="nestedatt--updated_by"></a>
### Nested Schema for `updated_by`

Required:

- `id` (String)

Read-Only:

- `api_token_name` (String)
- `avatar_url` (String)
- `full_name` (String)
- `id` (String)
- `subject_type` (String)
- `username` (String)
34 changes: 17 additions & 17 deletions examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ resource "astronomer_workspace" "tf_workspace" {
description = "This is a Terraform created workspace"
cicd_enforced_default = false
}
#
# output "terraform_workspace" {
# value = astronomer_workspace.tf_workspace
# }

output "terraform_workspace" {
value = astronomer_workspace.tf_workspace
}

// terraform import astronomer_workspace.imported_workspace cuid
# import {
# to = astronomer_workspace.imported_workspace
# id = "clukf7a2p000e01oe9pup199x"
# }
# resource "astronomer_workspace" "imported_workspace" {
# name = "imported_workspace"
# description = "hi fred"
# cicd_enforced_default = false
# }
#
# output "imported_workspace" {
# value = astronomer_workspace.imported_workspace
# }
import {
to = astronomer_workspace.imported_workspace
id = "clukhp501000401jdyc42imci"
}
resource "astronomer_workspace" "imported_workspace" {
name = "imported_workspace"
description = "hi fred"
cicd_enforced_default = false
}

output "imported_workspace" {
value = astronomer_workspace.imported_workspace
}
4 changes: 2 additions & 2 deletions internal/provider/datasources/data_source_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (d *workspaceDataSource) Read(
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var data models.WorkspaceDataSourceModel
var data models.WorkspaceDataSource

// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
Expand Down Expand Up @@ -107,7 +107,7 @@ func (d *workspaceDataSource) Read(
}

// Populate the model with the response data
models.FillWorkspaceDataSourceState(ctx, workspace.JSON200, &data)
data.ReadFromResponse(ctx, workspace.JSON200)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
27 changes: 15 additions & 12 deletions internal/provider/models/subject_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"context"
"github.com/hashicorp/terraform-plugin-framework/attr"

"github.com/astronomer/astronomer-terraform-provider/internal/clients/iam"
"github.com/astronomer/astronomer-terraform-provider/internal/clients/platform"
Expand All @@ -10,7 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
)

type SubjectProfileModel struct {
type SubjectProfile struct {
Id types.String `tfsdk:"id"`
SubjectType types.String `tfsdk:"subject_type"`
Username types.String `tfsdk:"username"`
Expand All @@ -19,10 +20,19 @@ type SubjectProfileModel struct {
ApiTokenName types.String `tfsdk:"api_token_name"`
}

var SubjectProfileTF map[string]attr.Type = map[string]attr.Type{
"id": types.StringType,
"subject_type": types.StringType,
"username": types.StringType,
"full_name": types.StringType,
"avatar_url": types.StringType,
"api_token_name": types.StringType,
}

func SubjectProfileTypesObject(
ctx context.Context,
basicSubjectProfile any,
) (*SubjectProfileModel, diag.Diagnostics) {
) (types.Object, diag.Diagnostics) {
// Check that the type passed in is a platform.BasicSubjectProfile or iam.BasicSubjectProfile
bsp, ok := basicSubjectProfile.(*platform.BasicSubjectProfile)
if !ok {
Expand All @@ -33,7 +43,7 @@ func SubjectProfileTypesObject(
"Unexpected type passed into subject profile",
map[string]interface{}{"value": basicSubjectProfile},
)
return nil, diag.Diagnostics{
return types.Object{}, diag.Diagnostics{
diag.NewErrorDiagnostic(
"Internal Error",
"SubjectProfileTypesObject expects a BasicSubjectProfile type but did not receive one",
Expand All @@ -51,7 +61,7 @@ func SubjectProfileTypesObject(
}
}

subjectProfile := SubjectProfileModel{
subjectProfile := SubjectProfile{
Id: types.StringValue(bsp.Id),
}

Expand All @@ -73,20 +83,13 @@ func SubjectProfileTypesObject(
} else {
subjectProfile.AvatarUrl = types.StringUnknown()
}
subjectProfile.ApiTokenName = types.StringNull()
} else {
if bsp.ApiTokenName != nil {
subjectProfile.ApiTokenName = types.StringValue(*bsp.ApiTokenName)
} else {
subjectProfile.ApiTokenName = types.StringUnknown()
}
}
} else {
subjectProfile.SubjectType = types.StringUnknown()
subjectProfile.Username = types.StringUnknown()
subjectProfile.FullName = types.StringUnknown()
subjectProfile.AvatarUrl = types.StringUnknown()
subjectProfile.ApiTokenName = types.StringUnknown()
}
return &subjectProfile, nil
return types.ObjectValueFrom(ctx, SubjectProfileTF, subjectProfile)
}
50 changes: 24 additions & 26 deletions internal/provider/models/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,35 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
)

// WorkspaceDataSourceModel describes the data source data model.
type WorkspaceDataSourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
OrganizationName types.String `tfsdk:"organization_name"`
CicdEnforcedDefault types.Bool `tfsdk:"cicd_enforced_default"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
CreatedBy *SubjectProfileModel `tfsdk:"created_by"`
UpdatedBy *SubjectProfileModel `tfsdk:"updated_by"`
// WorkspaceDataSource describes the data source data model.
type WorkspaceDataSource struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
OrganizationName types.String `tfsdk:"organization_name"`
CicdEnforcedDefault types.Bool `tfsdk:"cicd_enforced_default"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
CreatedBy types.Object `tfsdk:"created_by"`
UpdatedBy types.Object `tfsdk:"updated_by"`
}

// WorkspaceResourceModel describes the resource data model.
type WorkspaceResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
OrganizationName types.String `tfsdk:"organization_name"`
CicdEnforcedDefault types.Bool `tfsdk:"cicd_enforced_default"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
CreatedBy *SubjectProfileModel `tfsdk:"created_by"`
UpdatedBy *SubjectProfileModel `tfsdk:"updated_by"`
// WorkspaceResource describes the resource data model.
type WorkspaceResource struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Description types.String `tfsdk:"description"`
OrganizationName types.String `tfsdk:"organization_name"`
CicdEnforcedDefault types.Bool `tfsdk:"cicd_enforced_default"`
CreatedAt types.String `tfsdk:"created_at"`
UpdatedAt types.String `tfsdk:"updated_at"`
CreatedBy types.Object `tfsdk:"created_by"`
UpdatedBy types.Object `tfsdk:"updated_by"`
}

func FillWorkspaceResourceState(
func (data *WorkspaceResource) ReadFromResponse(
ctx context.Context,
workspace *platform.Workspace,
data *WorkspaceResourceModel,
) diag.Diagnostics {
data.Id = types.StringValue(workspace.Id)
data.Name = types.StringValue(workspace.Name)
Expand All @@ -63,10 +62,9 @@ func FillWorkspaceResourceState(
return nil
}

func FillWorkspaceDataSourceState(
func (data *WorkspaceDataSource) ReadFromResponse(
ctx context.Context,
workspace *platform.Workspace,
data *WorkspaceDataSourceModel,
) diag.Diagnostics {
data.Id = types.StringValue(workspace.Id)
data.Name = types.StringValue(workspace.Name)
Expand Down
14 changes: 7 additions & 7 deletions internal/provider/resources/resource_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *workspaceResource) Create(
req resource.CreateRequest,
resp *resource.CreateResponse,
) {
var data models.WorkspaceResourceModel
var data models.WorkspaceResource

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
Expand Down Expand Up @@ -110,7 +110,7 @@ func (r *workspaceResource) Create(
return
}

diags := models.FillWorkspaceResourceState(ctx, workspace.JSON200, &data)
diags := data.ReadFromResponse(ctx, workspace.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -127,7 +127,7 @@ func (r *workspaceResource) Read(
req resource.ReadRequest,
resp *resource.ReadResponse,
) {
var data models.WorkspaceResourceModel
var data models.WorkspaceResource

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
Expand Down Expand Up @@ -162,7 +162,7 @@ func (r *workspaceResource) Read(
return
}

diags := models.FillWorkspaceResourceState(ctx, workspace.JSON200, &data)
diags := data.ReadFromResponse(ctx, workspace.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -179,7 +179,7 @@ func (r *workspaceResource) Update(
req resource.UpdateRequest,
resp *resource.UpdateResponse,
) {
var data models.WorkspaceResourceModel
var data models.WorkspaceResource

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
Expand Down Expand Up @@ -214,7 +214,7 @@ func (r *workspaceResource) Update(
return
}

diags := models.FillWorkspaceResourceState(ctx, workspace.JSON200, &data)
diags := data.ReadFromResponse(ctx, workspace.JSON200)
if diags.HasError() {
resp.Diagnostics.Append(diags...)
return
Expand All @@ -231,7 +231,7 @@ func (r *workspaceResource) Delete(
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) {
var data models.WorkspaceResourceModel
var data models.WorkspaceResource

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
Expand Down
8 changes: 4 additions & 4 deletions internal/provider/schemas/subject_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = Describe("Common Test", func() {

DescribeTable(
"should return subject profile model",
func(input any, expected *models.SubjectProfileModel) {
func(input any, expected *models.SubjectProfile) {
subjectProfileModel, diags := models.SubjectProfileTypesObject(ctx, input)
Expect(diags.HasError()).To(BeFalse())
Expect(subjectProfileModel).To(Equal(expected))
Expand All @@ -46,7 +46,7 @@ var _ = Describe("Common Test", func() {
Id: "id",
SubjectType: (*platform.BasicSubjectProfileSubjectType)(lo.ToPtr("USER")),
Username: lo.ToPtr("username"),
}, &models.SubjectProfileModel{
}, &models.SubjectProfile{
Id: types.StringValue("id"),
SubjectType: types.StringValue("USER"),
Username: types.StringValue("username"),
Expand All @@ -58,7 +58,7 @@ var _ = Describe("Common Test", func() {
Id: "id",
SubjectType: (*iam.BasicSubjectProfileSubjectType)(lo.ToPtr("SERVICEKEY")),
ApiTokenName: lo.ToPtr("api_token_name"),
}, &models.SubjectProfileModel{
}, &models.SubjectProfile{
Id: types.StringValue("id"),
SubjectType: types.StringValue("SERVICEKEY"),
Username: types.StringNull(),
Expand All @@ -68,7 +68,7 @@ var _ = Describe("Common Test", func() {
}),
Entry("just id", &platform.BasicSubjectProfile{
Id: "id",
}, &models.SubjectProfileModel{
}, &models.SubjectProfile{
Id: types.StringValue("id"),
SubjectType: types.StringUnknown(),
Username: types.StringUnknown(),
Expand Down

0 comments on commit b5b8da0

Please sign in to comment.