-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f03ccc0
commit 9a7d58f
Showing
11 changed files
with
557 additions
and
2 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,44 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "bunnynet_compute_script_secret Resource - terraform-provider-bunnynet" | ||
subcategory: "" | ||
description: |- | ||
This resource manages a secret for a Compute Script in bunny.net. | ||
--- | ||
|
||
# bunnynet_compute_script_secret (Resource) | ||
|
||
This resource manages a secret for a Compute Script in bunny.net. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "bunnynet_compute_script_variable" "APP_SECRET" { | ||
script = bunnynet_compute_script.test.id | ||
name = "APP_ENV" | ||
value = "" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the secret. | ||
- `script` (Number) The ID of the associated compute script. | ||
- `value` (String, Sensitive) The value of the secret. | ||
|
||
### Read-Only | ||
|
||
- `id` (Number) The ID of the secret. | ||
|
||
## Import | ||
|
||
Import is supported using the following syntax: | ||
|
||
```shell | ||
terraform import bunnynet_compute_script_secret.test "1234|APP_SECRET" | ||
``` | ||
|
||
**NOTE**: When importing a secret, Terraform can't know the previously declared value, so it will be overwritten with the value you declare in your resource. To keep it from being overwritten, declare `value = ""`. |
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 @@ | ||
terraform import bunnynet_compute_script_secret.test "1234|APP_SECRET" |
5 changes: 5 additions & 0 deletions
5
examples/resources/bunnynet_compute_script_secret/resource.tf
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,5 @@ | ||
resource "bunnynet_compute_script_variable" "APP_SECRET" { | ||
script = bunnynet_compute_script.test.id | ||
name = "APP_ENV" | ||
value = "" | ||
} |
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 |
---|---|---|
@@ -1 +1 @@ | ||
terraform import bunnynet_compute_script_variable.test "$SCRIPT_ID|$VARIABLE_NAME" | ||
terraform import bunnynet_compute_script_variable.test "1234|APP_ENV" |
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,156 @@ | ||
// Copyright (c) BunnyWay d.o.o. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package api | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type ComputeScriptSecret struct { | ||
Id int64 `json:"Id,omitempty"` | ||
ScriptId int64 `json:"-"` | ||
Name string `json:"Name"` | ||
Value string `json:"Secret,omitempty"` | ||
} | ||
|
||
func (c *Client) GetComputeScriptSecretByName(scriptId int64, name string) (ComputeScriptSecret, error) { | ||
var data ComputeScriptSecret | ||
|
||
resp, err := c.doRequest(http.MethodGet, fmt.Sprintf("%s/compute/script/%d/secrets", c.apiUrl, scriptId), nil) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return data, errors.New(resp.Status) | ||
} | ||
|
||
bodyResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
var result struct { | ||
Secrets []ComputeScriptSecret `json:"Secrets"` | ||
} | ||
|
||
_ = resp.Body.Close() | ||
err = json.Unmarshal(bodyResp, &result) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
for _, secret := range result.Secrets { | ||
if secret.Name == name { | ||
secret.ScriptId = scriptId | ||
return secret, nil | ||
} | ||
} | ||
|
||
return data, errors.New("secret not found") | ||
} | ||
|
||
func (c *Client) CreateComputeScriptSecret(dataApi ComputeScriptSecret) (ComputeScriptSecret, error) { | ||
scriptId := dataApi.ScriptId | ||
bodyBytes, err := json.Marshal(dataApi) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
resp, err := c.doRequest(http.MethodPost, fmt.Sprintf("%s/compute/script/%d/secrets", c.apiUrl, scriptId), bytes.NewReader(bodyBytes)) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
bodyResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
_ = resp.Body.Close() | ||
var obj struct { | ||
Message string `json:"Message"` | ||
} | ||
|
||
err = json.Unmarshal(bodyResp, &obj) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
return ComputeScriptSecret{}, errors.New(obj.Message) | ||
} | ||
|
||
bodyResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
_ = resp.Body.Close() | ||
|
||
dataApiResult := ComputeScriptSecret{} | ||
err = json.Unmarshal(bodyResp, &dataApiResult) | ||
if err != nil { | ||
return dataApiResult, err | ||
} | ||
|
||
return c.GetComputeScriptSecretByName(scriptId, dataApiResult.Name) | ||
} | ||
|
||
func (c *Client) UpdateComputeScriptSecret(dataApi ComputeScriptSecret) (ComputeScriptSecret, error) { | ||
scriptId := dataApi.ScriptId | ||
|
||
// update attributes | ||
{ | ||
dataApi.Id = 0 | ||
body, err := json.Marshal(dataApi) | ||
|
||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
resp, err := c.doRequest(http.MethodPut, fmt.Sprintf("%s/compute/script/%d/secrets", c.apiUrl, scriptId), bytes.NewReader(body)) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
if resp.StatusCode != http.StatusNoContent { | ||
bodyResp, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
_ = resp.Body.Close() | ||
var obj struct { | ||
Message string `json:"Message"` | ||
} | ||
|
||
err = json.Unmarshal(bodyResp, &obj) | ||
if err != nil { | ||
return ComputeScriptSecret{}, err | ||
} | ||
|
||
return ComputeScriptSecret{}, errors.New(obj.Message) | ||
} | ||
} | ||
|
||
return c.GetComputeScriptSecretByName(scriptId, dataApi.Name) | ||
} | ||
|
||
func (c *Client) DeleteComputeScriptSecret(scriptId int64, id int64) error { | ||
resp, err := c.doRequest(http.MethodDelete, fmt.Sprintf("%s/compute/script/%d/secrets/%d", c.apiUrl, scriptId, id), nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.StatusCode != http.StatusNoContent { | ||
return errors.New(resp.Status) | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.