-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add circleci_environment_variable resource
- Loading branch information
Showing
6 changed files
with
612 additions
and
8 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
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,113 @@ | ||
package circlecigo | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/dghubble/sling" | ||
"net/http" | ||
) | ||
|
||
type EnvironmentVariableService struct { | ||
sling *sling.Sling | ||
token string | ||
} | ||
|
||
func newEnvironmentVariableService(sling *sling.Sling, token string) *EnvironmentVariableService { | ||
return &EnvironmentVariableService{ | ||
sling: sling.New(), | ||
token: token, | ||
} | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
// Input | ||
// ----------------------------------------------------------------------------- | ||
|
||
type EnvironmentVariable struct { | ||
ProjectId string | ||
Name string | ||
Value string | ||
ValueMasked string | ||
} | ||
|
||
type environmentVariableInputCreate struct { | ||
Name string `json:"name"` | ||
Value string `json:"value"` | ||
} | ||
|
||
type environmentVariableResponseType struct { | ||
Name string `json:"name"` | ||
ValueMasked string `json:"value"` | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
// CRUD | ||
// ----------------------------------------------------------------------------- | ||
|
||
func (service *EnvironmentVariableService) Create(environmentVariable *EnvironmentVariable) (*EnvironmentVariable, *http.Response, error) { | ||
if environmentVariable == nil { | ||
return nil, nil, fmt.Errorf("Cannot create an environment variable that is nil") | ||
} | ||
|
||
requestBody := &environmentVariableInputCreate{Name: environmentVariable.Name, Value: environmentVariable.Value} | ||
|
||
environmentVariableResponse := new(environmentVariableResponseType) | ||
genericError := new(json.RawMessage) | ||
resp, err := service.sling.New().Set("Accept", "application/json").Post("project/"+environmentVariable.ProjectId+"/envvar?circle-token="+service.token).BodyJSON(requestBody).Receive(environmentVariableResponse, genericError) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
finalErr := relevantErrorFromStatusCode(resp, err) | ||
var responseEnvironmentVariable *EnvironmentVariable = nil | ||
if finalErr == nil { | ||
responseEnvironmentVariable = &EnvironmentVariable{ | ||
ProjectId: environmentVariable.ProjectId, | ||
Name: environmentVariableResponse.Name, | ||
Value: environmentVariable.Value, | ||
ValueMasked: environmentVariableResponse.ValueMasked, | ||
} | ||
} | ||
|
||
return responseEnvironmentVariable, resp, finalErr | ||
} | ||
|
||
func (service *EnvironmentVariableService) Read(environmentVariable *EnvironmentVariable) (*EnvironmentVariable, *http.Response, error) { | ||
if environmentVariable == nil { | ||
return nil, nil, fmt.Errorf("Cannot read an environment variable that is nil") | ||
} | ||
|
||
environmentVariableResponse := new(environmentVariableResponseType) | ||
genericError := new(json.RawMessage) | ||
resp, err := service.sling.New().Set("Accept", "application/json").Get("project/"+environmentVariable.ProjectId+"/envvar/"+environmentVariable.Name+"?circle-token="+service.token).Receive(environmentVariableResponse, genericError) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
finalErr := relevantErrorFromStatusCode(resp, err) | ||
var responseEnvironmentVariable *EnvironmentVariable = nil | ||
if finalErr == nil { | ||
responseEnvironmentVariable = &EnvironmentVariable{ | ||
ProjectId: environmentVariable.ProjectId, | ||
Name: environmentVariableResponse.Name, | ||
Value: environmentVariable.Value, | ||
ValueMasked: environmentVariableResponse.ValueMasked, | ||
} | ||
} | ||
|
||
return responseEnvironmentVariable, resp, finalErr | ||
} | ||
|
||
func (service *EnvironmentVariableService) Delete(environmentVariable *EnvironmentVariable) (*http.Response, error) { | ||
if environmentVariable == nil { | ||
return nil, fmt.Errorf("Cannot delete an environment variable that is nil") | ||
} | ||
|
||
req, reqErr := service.sling.New().Delete("project/" + environmentVariable.ProjectId + "/envvar/" + environmentVariable.Name + "?circle-token=" + service.token).Request() | ||
if reqErr != nil { | ||
return nil, reqErr | ||
} | ||
resp, httpErr := http.DefaultClient.Do(req) | ||
|
||
return resp, relevantErrorFromStatusCode(resp, httpErr) | ||
} |
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,102 @@ | ||
package circleci | ||
|
||
import ( | ||
"fmt" | ||
circleci "github.com/edahlseng/terraform-provider-circleci/circleci/circleci-go" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceCircleCiEnvironmentVariable() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceCircleCiEnvironmentVariableCreate, | ||
Read: resourceCircleCiEnvironmentVariableRead, | ||
Delete: resourceCircleCiEnvironmentVariableDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"project_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCircleCiEnvironmentVariableCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*circleci.Client) | ||
|
||
environmentVariableInput := &circleci.EnvironmentVariable{ | ||
ProjectId: d.Get("project_id").(string), | ||
Name: d.Get("name").(string), | ||
Value: d.Get("value").(string), | ||
} | ||
|
||
environmentVariable, _, err := client.EnvironmentVariables.Create(environmentVariableInput) | ||
if err != nil { | ||
return fmt.Errorf("Error creating CircleCI environment variable: %s", err) | ||
} | ||
|
||
if environmentVariable == nil { | ||
return fmt.Errorf("Error creating CircleCI environment variable: environment variable is nil") | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/%s", environmentVariable.ProjectId, environmentVariable.Name)) | ||
d.Set("project_id", environmentVariable.ProjectId) | ||
d.Set("name", environmentVariable.Name) | ||
d.Set("value", environmentVariable.Value) | ||
d.Set("value_masked", environmentVariable.ValueMasked) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCircleCiEnvironmentVariableRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*circleci.Client) | ||
|
||
environmentVariableInput := &circleci.EnvironmentVariable{ | ||
ProjectId: d.Get("project_id").(string), | ||
Name: d.Get("name").(string), | ||
} | ||
|
||
environmentVariable, _, err := client.EnvironmentVariables.Read(environmentVariableInput) | ||
if err != nil { | ||
return fmt.Errorf("Error reading CircleCI environment variable: %s", err) | ||
} | ||
|
||
if environmentVariable == nil { | ||
return fmt.Errorf("Error creating CircleCI environment variable: environment variable is nil") | ||
} | ||
|
||
d.Set("name", environmentVariable.Name) | ||
d.Set("value_masked", environmentVariable.ValueMasked) | ||
|
||
return nil | ||
} | ||
|
||
func resourceCircleCiEnvironmentVariableDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*circleci.Client) | ||
|
||
environmentVariableInput := &circleci.EnvironmentVariable{ | ||
ProjectId: d.Get("project_id").(string), | ||
Name: d.Get("name").(string), | ||
Value: "", | ||
ValueMasked: "", | ||
} | ||
|
||
_, err := client.EnvironmentVariables.Delete(environmentVariableInput) | ||
if err != nil { | ||
return fmt.Errorf("Error deleting environment variable: %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.