Skip to content

Commit

Permalink
Merge pull request #34 from Infisical/daniel/project-interactions
Browse files Browse the repository at this point in the history
Feat: Add projects to Terraform Provider
  • Loading branch information
maidul98 authored Mar 27, 2024
2 parents 32ef5d4 + e2fa5d8 commit 38cfd8f
Show file tree
Hide file tree
Showing 12 changed files with 796 additions and 4 deletions.
86 changes: 86 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,89 @@ func (client Client) CallGetSingleRawSecretByNameV3(request GetSingleSecretByNam

return secretsResponse, nil
}

func (client Client) CallCreateProject(request CreateProjectRequest) (CreateProjectResponse, error) {

if request.Slug == "" {
request = CreateProjectRequest{
ProjectName: request.ProjectName,
OrganizationSlug: request.OrganizationSlug,
}
}

var projectResponse CreateProjectResponse
response, err := client.Config.HttpClient.
R().
SetResult(&projectResponse).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Post("api/v2/workspace")

if err != nil {
return CreateProjectResponse{}, fmt.Errorf("CallCreateProject: Unable to complete api request [err=%s]", err)
}

if response.IsError() {
return CreateProjectResponse{}, fmt.Errorf("CallCreateProject: Unsuccessful response. [response=%s]", response)
}

return projectResponse, nil
}

func (client Client) CallDeleteProject(request DeleteProjectRequest) error {
var projectResponse DeleteProjectResponse
response, err := client.Config.HttpClient.
R().
SetResult(&projectResponse).
SetHeader("User-Agent", USER_AGENT).
Delete(fmt.Sprintf("api/v2/workspace/%s", request.Slug))

if err != nil {
return fmt.Errorf("CallDeleteProject: Unable to complete api request [err=%s]", err)
}

if response.IsError() {
return fmt.Errorf("CallDeleteProject: Unsuccessful response. [response=%s]", response)
}

return nil
}

func (client Client) CallGetProject(request GetProjectRequest) (ProjectWithEnvironments, error) {
var projectResponse ProjectWithEnvironments
response, err := client.Config.HttpClient.
R().
SetResult(&projectResponse).
SetHeader("User-Agent", USER_AGENT).
Get(fmt.Sprintf("api/v2/workspace/%s", request.Slug))

if err != nil {
return ProjectWithEnvironments{}, fmt.Errorf("CallGetProject: Unable to complete api request [err=%s]", err)
}

if response.IsError() {
return ProjectWithEnvironments{}, fmt.Errorf("CallGetProject: Unsuccessful response. [response=%s]", response)
}

return projectResponse, nil
}

func (client Client) CallUpdateProject(request UpdateProjectRequest) (UpdateProjectResponse, error) {
var projectResponse UpdateProjectResponse
response, err := client.Config.HttpClient.
R().
SetResult(&projectResponse).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Patch(fmt.Sprintf("api/v2/workspace/%s", request.Slug))

if err != nil {
return UpdateProjectResponse{}, fmt.Errorf("CallUpdateProject: Unable to complete api request [err=%s]", err)
}

if response.IsError() {
return UpdateProjectResponse{}, fmt.Errorf("CallUpdateProject: Unsuccessful response. [response=%s]", response)
}

return projectResponse, nil
}
61 changes: 61 additions & 0 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,48 @@ type EncryptedSecretV3 struct {
UpdatedAt time.Time `json:"updatedAt"`
}

type Project struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
AutoCapitalization bool `json:"autoCapitalization"`
OrgID string `json:"orgId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Version int `json:"version"`

UpgradeStatus string `json:"upgradeStatus"` // can be null. if its null it will be converted to an empty string.
}

type ProjectWithEnvironments struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
AutoCapitalization bool `json:"autoCapitalization"`
OrgID string `json:"orgId"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
Version int64 `json:"version"`
UpgradeStatus string `json:"upgradeStatus"`
Environments []ProjectEnvironment `json:"environments"`
}

type ProjectEnvironment struct {
Name string `json:"name"`
Slug string `json:"slug"`
ID string `json:"id"`
}

type CreateProjectResponse struct {
Project Project `json:"project"`
}

type DeleteProjectResponse struct {
Project Project `json:"workspace"`
}

type UpdateProjectResponse Project

type GetEncryptedSecretsV3Response struct {
Secrets []EncryptedSecretV3 `json:"secrets"`
}
Expand Down Expand Up @@ -232,3 +274,22 @@ type UpdateRawSecretByNameV3Request struct {
SecretPath string `json:"secretPath"`
SecretValue string `json:"secretValue"`
}

type CreateProjectRequest struct {
ProjectName string `json:"projectName"`
Slug string `json:"slug"`
OrganizationSlug string `json:"organizationSlug"`
}

type DeleteProjectRequest struct {
Slug string `json:"slug"`
}

type GetProjectRequest struct {
Slug string `json:"slug"`
}

type UpdateProjectRequest struct {
Slug string `json:"slug"`
ProjectName string `json:"name"`
}
72 changes: 72 additions & 0 deletions docs/data-sources/projects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "infisical_projects Data Source - terraform-provider-infisical"
subcategory: ""
description: |-
Interact with Infisical projects. Only Machine Identity authentication is supported for this data source.
---

# infisical_projects (Data Source)

Interact with Infisical projects. Only Machine Identity authentication is supported for this data source.

## Example Usage

```terraform
terraform {
required_providers {
infisical = {
# version = <latest version>
source = "infisical/infisical"
}
}
}
provider "infisical" {
host = "https://app.infisical.com" # Only required if using self hosted instance of Infisical, default is https://app.infisical.com
client_id = "<machine-identity-client-id>"
client_secret = "<machine-identity-client-secret>"
}
data "infisical_projects" "test-project" {
slug = "<project-slug>"
}
// Get the value of the "dev" environment
output "dev-environment" {
value = data.infisical_projects.test-project.environments["dev"]
}
// Get the entire project
output "entire-project" {
value = data.infisical_projects.test-project
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `slug` (String) The slug of the project to fetch

### Read-Only

- `auto_capitalization` (Boolean) The auto capitalization status of the project
- `created_at` (String) The creation date of the project
- `environments` (Attributes Map) (see [below for nested schema](#nestedatt--environments))
- `id` (String) The ID of the project
- `name` (String) The name of the project
- `org_id` (String) The ID of the organization to which the project belongs
- `updated_at` (String) The last update date of the project
- `upgrade_status` (String) The upgrade status of the project
- `version` (Number) The version of the project

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

Read-Only:

- `id` (String) The ID of the environment
- `name` (String) The name of the environment
- `slug` (String) The slug of the environment
4 changes: 2 additions & 2 deletions docs/data-sources/secrets.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "infisical_secrets Data Source - terraform-provider-infisical"
subcategory: ""
description: |-
Get secrets from Infisical
Interact with Infisical secrets
---

# infisical_secrets (Data Source)

Get secrets from Infisical
Interact with Infisical secrets

## Example Usage

Expand Down
57 changes: 57 additions & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "infisical_project Resource - terraform-provider-infisical"
subcategory: ""
description: |-
Create projects & save to Infisical. Only Machine Identity authentication is supported for this data source.
---

# infisical_project (Resource)

Create projects & save to Infisical. Only Machine Identity authentication is supported for this data source.

## Example Usage

```terraform
terraform {
required_providers {
infisical = {
# version = <latest version>
source = "infisical/infisical"
}
}
}
provider "infisical" {
host = "https://app.infisical.com" # Only required if using self hosted instance of Infisical, default is https://app.infisical.com
client_id = "<machine-identity-client-id>"
client_secret = "<machine-identity-client-secret>"
}
resource "infisical_project" "gcp-project" {
name = "GCP Project"
slug = "gcp-project"
}
resource "infisical_project" "aws-project" {
name = "AWS Project"
slug = "aws-project"
}
resource "infisical_project" "azure-project" {
name = "Azure Project"
slug = "azure-project"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of the project
- `slug` (String) The slug of the project

### Read-Only

- `last_updated` (String)
28 changes: 28 additions & 0 deletions examples/data-sources/infisical_projects/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
terraform {
required_providers {
infisical = {
# version = <latest version>
source = "infisical/infisical"
}
}
}

provider "infisical" {
host = "https://app.infisical.com" # Only required if using self hosted instance of Infisical, default is https://app.infisical.com
client_id = "<machine-identity-client-id>"
client_secret = "<machine-identity-client-secret>"
}

data "infisical_projects" "test-project" {
slug = "<project-slug>"
}

// Get the value of the "dev" environment
output "dev-environment" {
value = data.infisical_projects.test-project.environments["dev"]
}

// Get the entire project
output "entire-project" {
value = data.infisical_projects.test-project
}
31 changes: 31 additions & 0 deletions examples/resources/infisical_project/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
terraform {
required_providers {
infisical = {
# version = <latest version>
source = "infisical/infisical"
}
}
}

provider "infisical" {
host = "https://app.infisical.com" # Only required if using self hosted instance of Infisical, default is https://app.infisical.com
client_id = "<machine-identity-client-id>"
client_secret = "<machine-identity-client-secret>"
}

resource "infisical_project" "gcp-project" {
name = "GCP Project"
slug = "gcp-project"
}

resource "infisical_project" "aws-project" {
name = "AWS Project"
slug = "aws-project"
}

resource "infisical_project" "azure-project" {
name = "Azure Project"
slug = "azure-project"
}


Loading

0 comments on commit 38cfd8f

Please sign in to comment.