Skip to content

Commit

Permalink
Merge pull request #47 from Infisical/feat/project-environment-resource
Browse files Browse the repository at this point in the history
feat: project environment resource
  • Loading branch information
sheensantoscapadngan authored Jul 23, 2024
2 parents b8baf10 + e723fed commit 0eb1878
Show file tree
Hide file tree
Showing 6 changed files with 465 additions and 0 deletions.
54 changes: 54 additions & 0 deletions docs/resources/project_environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "infisical_project_environment Resource - terraform-provider-infisical"
subcategory: ""
description: |-
Create project environment
---

# infisical_project_environment (Resource)

Create project environment

## 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 = "<>"
client_secret = "<>"
}
resource "infisical_project" "example" {
name = "example"
slug = "example"
}
resource "infisical_project_environment" "pre-prod" {
name = "pre-prod"
project_id = infisical_project.example.id
slug = "preprod"
}
```

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

### Required

- `name` (String) The name of the environment
- `project_id` (String) The Infisical project ID (Required for Machine Identity auth, and service tokens with multiple scopes)
- `slug` (String) The slug of the environment

### Read-Only

- `id` (String) The ID of the environment
25 changes: 25 additions & 0 deletions examples/resources/infisical_project_environment/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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 = "<>"
client_secret = "<>"
}

resource "infisical_project" "example" {
name = "example"
slug = "example"
}

resource "infisical_project_environment" "pre-prod" {
name = "pre-prod"
project_id = infisical_project.example.id
slug = "preprod"
}
39 changes: 39 additions & 0 deletions internal/client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,3 +756,42 @@ type ListSecretFolderRequest struct {
type ListSecretFolderResponse struct {
Folders []SecretFolder `json:"folders"`
}

type CreateProjectEnvironmentRequest struct {
Name string `json:"name"`
Slug string `json:"slug"`
ProjectID string `json:"workspaceId"`
}

type CreateProjectEnvironmentResponse struct {
Environment ProjectEnvironment `json:"environment"`
}

type DeleteProjectEnvironmentRequest struct {
ID string `json:"id"`
ProjectID string `json:"workspaceId"`
}

type DeleteProjectEnvironmentResponse struct {
Environment ProjectEnvironment `json:"environment"`
}

type GetProjectEnvironmentByIDRequest struct {
ID string `json:"id"`
ProjectID string `json:"workspaceId"`
}

type GetProjectEnvironmentByIDResponse struct {
Environment ProjectEnvironment `json:"environment"`
}

type UpdateProjectEnvironmentRequest struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
ProjectID string `json:"workspaceId"`
}

type UpdateProjectEnvironmentResponse struct {
Environment ProjectEnvironment `json:"environment"`
}
91 changes: 91 additions & 0 deletions internal/client/project_environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package infisicalclient

import (
"fmt"
"net/http"
)

func (client Client) CreateProjectEnvironment(request CreateProjectEnvironmentRequest) (CreateProjectEnvironmentResponse, error) {
var body CreateProjectEnvironmentResponse
response, err := client.Config.HttpClient.
R().
SetResult(&body).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Post(fmt.Sprintf("api/v1/workspace/%s/environments", request.ProjectID))

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

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

return body, nil
}

func (client Client) DeleteProjectEnvironment(request DeleteProjectEnvironmentRequest) (DeleteProjectEnvironmentResponse, error) {
var body DeleteProjectEnvironmentResponse
response, err := client.Config.HttpClient.
R().
SetResult(&body).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Delete(fmt.Sprintf("api/v1/workspace/%s/environments/%s", request.ProjectID, request.ID))

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

if response.IsError() {
return DeleteProjectEnvironmentResponse{}, fmt.Errorf("CallDeleteProjectEnvironment Unsuccessful response. [response=%s]", string(response.Body()))
}

return body, nil
}

func (client Client) GetProjectEnvironmentByID(request GetProjectEnvironmentByIDRequest) (GetProjectEnvironmentByIDResponse, error) {
var body GetProjectEnvironmentByIDResponse

httpRequest := client.Config.HttpClient.
R().
SetResult(&body).
SetHeader("User-Agent", USER_AGENT)

response, err := httpRequest.Get(fmt.Sprintf("api/v1/workspace/%s/environments/%s", request.ProjectID, request.ID))

if response.StatusCode() == http.StatusNotFound {
return GetProjectEnvironmentByIDResponse{}, ErrNotFound
}

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

if response.IsError() {
return GetProjectEnvironmentByIDResponse{}, fmt.Errorf("GetProjectEnvironmentByID: Unsuccessful response. [response=%v]", string(response.Body()))
}

return body, nil
}

func (client Client) UpdateProjectEnvironment(request UpdateProjectEnvironmentRequest) (UpdateProjectEnvironmentResponse, error) {
var body UpdateProjectEnvironmentResponse
response, err := client.Config.HttpClient.
R().
SetResult(&body).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Patch(fmt.Sprintf("api/v1/workspace/%s/environments/%s", request.ProjectID, request.ID))

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

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

return body, nil
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ func (p *infisicalProvider) Resources(_ context.Context) []func() resource.Resou
infisicalResource.NewProjectIdentitySpecificPrivilegeResource,
infisicalResource.NewProjectSecretTagResource,
infisicalResource.NewProjectSecretFolderResource,
infisicalResource.NewProjectEnvironmentResource,
}
}
Loading

0 comments on commit 0eb1878

Please sign in to comment.