generated from hashicorp/terraform-provider-scaffolding-framework
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from kelvintaywl/feat-project-resource
Feat: support project
- Loading branch information
Showing
10 changed files
with
372 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
page_title: "circleci_project Resource - terraform-provider-circleci" | ||
subcategory: "" | ||
description: |- | ||
Manages a project | ||
--- | ||
|
||
# circleci_project (Resource) | ||
|
||
Manages a project | ||
|
||
## Assumption | ||
|
||
- The underlying repository on GitHub / Bitbucket has a .circleci/config.yml file in its default branch. | ||
|
||
## Important | ||
|
||
CircleCI projects **cannot be deleted**. | ||
When you run `terraform destroy`, it will not destroy the project on CircleCI. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
# set up a new CircleCI project | ||
# ASSUMPTION: the GitHub project has a .circleci/config.yml on its default branch | ||
resource "circleci_project" "my_project" { | ||
slug = "github/acme/foobar" | ||
} | ||
# add a project env var to this project | ||
resource "circleci_env_var" "my_env_var" { | ||
project_slug = circleci_project.my_project.slug | ||
name = "FOOBAR" | ||
value = "0Cme2FmlXk" | ||
} | ||
output "vcs_url" { | ||
description = "VCS url" | ||
value = circleci_project.my_project.vcs_url | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `slug` (String) Project slug in the form `vcs-slug/org-name/repo-name`. The / characters may be URL-escaped. | ||
|
||
### Read-Only | ||
|
||
- `id` (String) Read-only unique identifier | ||
- `name` (String) Name of the project | ||
- `organization_id` (String) The id of the organization the project belongs to | ||
- `organization_name` (String) The name of the organization the project belongs to | ||
- `organization_slug` (String) The slug of the organization the project belongs to | ||
- `vcs_default_branch` (String) Default branch of this project | ||
- `vcs_provider` (String) VCS provider (either GitHub, Bitbucket or CircleCI) | ||
- `vcs_url` (String) URL to the repository hosting the project's code |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# set up a new CircleCI project | ||
# ASSUMPTION: the GitHub project has a .circleci/config.yml on its default branch | ||
resource "circleci_project" "my_project" { | ||
slug = "github/acme/foobar" | ||
} | ||
|
||
# add a project env var to this project | ||
resource "circleci_env_var" "my_env_var" { | ||
project_slug = circleci_project.my_project.slug | ||
name = "FOOBAR" | ||
value = "0Cme2FmlXk" | ||
} | ||
|
||
output "vcs_url" { | ||
description = "VCS url" | ||
value = circleci_project.my_project.vcs_url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
|
||
"github.com/kelvintaywl/circleci-go-sdk/client/project" | ||
) | ||
|
||
// Ensure provider defined types fully satisfy framework interfaces | ||
var _ resource.Resource = &ProjectResource{} | ||
|
||
func NewProjectResource() resource.Resource { | ||
return &ProjectResource{} | ||
} | ||
|
||
type ProjectResource struct { | ||
client *CircleciAPIClient | ||
} | ||
|
||
type ProjectResourceModel struct { | ||
Id types.String `tfsdk:"id"` | ||
Slug types.String `tfsdk:"slug"` | ||
Name types.String `tfsdk:"name"` | ||
OrganizationName types.String `tfsdk:"organization_name"` | ||
OrganizationSlug types.String `tfsdk:"organization_slug"` | ||
OrganizationId types.String `tfsdk:"organization_id"` | ||
VcsProvider types.String `tfsdk:"vcs_provider"` | ||
VcsDefaultBranch types.String `tfsdk:"vcs_default_branch"` | ||
VcsURL types.String `tfsdk:"vcs_url"` | ||
} | ||
|
||
func (r *ProjectResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_project" | ||
} | ||
|
||
func (r *ProjectResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
MarkdownDescription: "Manages a project", | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
MarkdownDescription: "Read-only unique identifier", | ||
Computed: true, | ||
// unchanged even during updates | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.UseStateForUnknown(), | ||
}, | ||
}, | ||
"slug": schema.StringAttribute{ | ||
MarkdownDescription: "Project slug in the form `vcs-slug/org-name/repo-name`. The / characters may be URL-escaped.", | ||
Required: true, | ||
}, | ||
"name": schema.StringAttribute{ | ||
MarkdownDescription: "Name of the project", | ||
Computed: true, | ||
}, | ||
"organization_name": schema.StringAttribute{ | ||
MarkdownDescription: "The name of the organization the project belongs to", | ||
Computed: true, | ||
}, | ||
"organization_slug": schema.StringAttribute{ | ||
MarkdownDescription: "The slug of the organization the project belongs to", | ||
Computed: true, | ||
}, | ||
"organization_id": schema.StringAttribute{ | ||
MarkdownDescription: "The id of the organization the project belongs to", | ||
Computed: true, | ||
}, | ||
"vcs_url": schema.StringAttribute{ | ||
MarkdownDescription: "URL to the repository hosting the project's code", | ||
Computed: true, | ||
}, | ||
"vcs_provider": schema.StringAttribute{ | ||
MarkdownDescription: "VCS provider (either GitHub, Bitbucket or CircleCI)", | ||
Computed: true, | ||
}, | ||
"vcs_default_branch": schema.StringAttribute{ | ||
MarkdownDescription: "Default branch of this project", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Configure adds the provider configured client to the data source. | ||
func (r *ProjectResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { | ||
// Prevent panic if the provider has not been configured. | ||
if req.ProviderData == nil { | ||
return | ||
} | ||
|
||
client, ok := req.ProviderData.(*CircleciAPIClient) | ||
|
||
if !ok { | ||
resp.Diagnostics.AddError( | ||
"Unexpected Resource Configure Type", | ||
fmt.Sprintf("Expected *CircleciAPIClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), | ||
) | ||
|
||
return | ||
} | ||
|
||
r.client = client | ||
} | ||
|
||
// Read refreshes the Terraform state with the latest data. | ||
func (r *ProjectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
// Get current state | ||
var state ProjectResourceModel | ||
diags := req.State.Get(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
projectSlug := state.Slug.ValueString() | ||
param := project.NewGetProjectParamsWithContext(ctx).WithDefaults() | ||
param = param.WithProjectSlug(projectSlug) | ||
|
||
res, err := r.client.Client.Project.GetProject(param, r.client.Auth) | ||
if err != nil { | ||
resp.Diagnostics.AddError(fmt.Sprintf("Encountered error reading Project(%s)", projectSlug), fmt.Sprintf("%s", err)) | ||
return | ||
} | ||
|
||
pj := res.GetPayload() | ||
state.Id = types.StringValue(pj.ID.String()) | ||
state.Name = types.StringValue(pj.Name) | ||
state.OrganizationName = types.StringValue(pj.OrganizationName) | ||
state.OrganizationSlug = types.StringValue(pj.OrganizationSlug) | ||
state.OrganizationId = types.StringValue(pj.OrganizationID.String()) | ||
state.VcsProvider = types.StringValue(pj.VcsInfo.Provider) | ||
state.VcsDefaultBranch = types.StringValue(pj.VcsInfo.DefaultBranch) | ||
state.VcsURL = types.StringValue(pj.VcsInfo.VcsURL) | ||
|
||
diags = resp.State.Set(ctx, &state) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
// Create creates the resource and sets the initial Terraform state. | ||
func (r *ProjectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
// Retrieve values from plan | ||
var plan ProjectResourceModel | ||
diags := req.Plan.Get(ctx, &plan) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
projectSlug := plan.Slug.ValueString() | ||
url := fmt.Sprintf("https://%s/api/v1.1/project/%s/follow", r.client.Hostname, projectSlug) | ||
request, err := http.NewRequest(http.MethodPost, url, nil) | ||
if err != nil { | ||
resp.Diagnostics.AddError("Encountered error setting up API call", fmt.Sprintf("%s", err)) | ||
} | ||
res, err := r.client.V1Client.Do(request) | ||
if err != nil || res.StatusCode != 200 { | ||
resp.Diagnostics.AddError(fmt.Sprintf("Encountered error following project (%s)", projectSlug), fmt.Sprintf("%s", err)) | ||
} | ||
|
||
// read | ||
readParam := project.NewGetProjectParamsWithContext(ctx).WithDefaults() | ||
readParam = readParam.WithProjectSlug(projectSlug) | ||
|
||
readRes, err := r.client.Client.Project.GetProject(readParam, r.client.Auth) | ||
if err != nil { | ||
resp.Diagnostics.AddError(fmt.Sprintf("Encountered error reading Project(%s)", projectSlug), fmt.Sprintf("%s", err)) | ||
return | ||
} | ||
|
||
pj := readRes.GetPayload() | ||
plan.Id = types.StringValue(pj.ID.String()) | ||
plan.Name = types.StringValue(pj.Name) | ||
plan.OrganizationName = types.StringValue(pj.OrganizationName) | ||
plan.OrganizationSlug = types.StringValue(pj.OrganizationSlug) | ||
plan.OrganizationId = types.StringValue(pj.OrganizationID.String()) | ||
plan.VcsProvider = types.StringValue(pj.VcsInfo.Provider) | ||
plan.VcsDefaultBranch = types.StringValue(pj.VcsInfo.DefaultBranch) | ||
plan.VcsURL = types.StringValue(pj.VcsInfo.VcsURL) | ||
|
||
diags = resp.State.Set(ctx, &plan) | ||
resp.Diagnostics.Append(diags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
} | ||
|
||
func (r *ProjectResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
// not implemented; not possible to update project | ||
tflog.Warn(ctx, "Project cannot be updated via this provider.") | ||
} | ||
|
||
func (r *ProjectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
// not implemented; not possible to delete project | ||
tflog.Warn(ctx, "Project cannot be deleted via this provider.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package provider | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccProjectResource(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, | ||
Steps: []resource.TestStep{ | ||
// Create and Read testing | ||
{ | ||
Config: providerConfig + fmt.Sprintf(` | ||
resource "circleci_project" "p1" { | ||
slug = "%s" | ||
} | ||
`, projectSlug), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("circleci_project.p1", "slug", projectSlug), | ||
|
||
resource.TestCheckResourceAttr("circleci_project.p1", "organization_name", "kelvintaywl-tf"), | ||
resource.TestCheckResourceAttr("circleci_project.p1", "organization_slug", "gh/kelvintaywl-tf"), | ||
resource.TestCheckResourceAttrSet("circleci_project.p1", "organization_id"), | ||
|
||
resource.TestCheckResourceAttr("circleci_project.p1", "vcs_url", "https://github.com/kelvintaywl-tf/tf-provider-acceptance-test-dummy"), | ||
resource.TestCheckResourceAttr("circleci_project.p1", "vcs_default_branch", "main"), | ||
resource.TestCheckResourceAttr("circleci_project.p1", "vcs_provider", "GitHub"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.