Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add projects to Terraform Provider #34

Merged
merged 14 commits into from
Mar 27, 2024
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)
DanielHougaard marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@
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 @@ -82,12 +124,12 @@
AuthTag []byte `json:"AuthTag"`
}

// Workspace key request

Check failure on line 127 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type GetEncryptedWorkspaceKeyRequest struct {
WorkspaceId string `json:"workspaceId"`
}

// Workspace key response

Check failure on line 132 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type GetEncryptedWorkspaceKeyResponse struct {
ID string `json:"_id"`
EncryptedKey string `json:"encryptedKey"`
Expand All @@ -110,7 +152,7 @@
UpdatedAt time.Time `json:"updatedAt"`
}

// encrypted secret

Check failure on line 155 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type EncryptedSecret struct {
SecretName string `json:"secretName"`
WorkspaceID string `json:"workspaceId"`
Expand All @@ -128,7 +170,7 @@
SecretPath string `json:"secretPath"`
}

// create secrets

Check failure on line 173 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type CreateSecretV3Request struct {
SecretName string `json:"secretName"`
WorkspaceID string `json:"workspaceId"`
Expand All @@ -146,7 +188,7 @@
SecretPath string `json:"secretPath"`
}

// delete secret by name api

Check failure on line 191 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type DeleteSecretV3Request struct {
SecretName string `json:"secretName"`
WorkspaceId string `json:"workspaceId"`
Expand All @@ -155,7 +197,7 @@
SecretPath string `json:"secretPath"`
}

// update secret by name api

Check failure on line 200 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type UpdateSecretByNameV3Request struct {
SecretName string `json:"secretName"`
WorkspaceID string `json:"workspaceId"`
Expand All @@ -167,7 +209,7 @@
SecretValueTag string `json:"secretValueTag"`
}

// get secret by name api

Check failure on line 212 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type GetSingleSecretByNameV3Request struct {
SecretName string `json:"secretName"`
WorkspaceId string `json:"workspaceId"`
Expand Down Expand Up @@ -204,7 +246,7 @@
Secret RawV3Secret `json:"secret"`
}

// create secrets

Check failure on line 249 in client/model.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
type CreateRawSecretV3Request struct {
WorkspaceID string `json:"workspaceId"`
Type string `json:"type"`
Expand Down Expand Up @@ -232,3 +274,22 @@
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
DanielHougaard marked this conversation as resolved.
Show resolved Hide resolved

### 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
Loading