-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
env0_ssh_key resource and data source
- Loading branch information
1 parent
655214c
commit e03702d
Showing
15 changed files
with
384 additions
and
51 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,49 @@ | ||
package env0apiclient | ||
|
||
import ( | ||
"errors" | ||
"net/url" | ||
) | ||
|
||
func (self *ApiClient) SshKeyCreate(payload SshKeyCreatePayload) (SshKey, error) { | ||
if payload.Name == "" { | ||
return SshKey{}, errors.New("Must specify ssh key name on creation") | ||
} | ||
if payload.Value == "" { | ||
return SshKey{}, errors.New("Must specify ssh key value (private key in PEM format) on creation") | ||
} | ||
if payload.OrganizationId != "" { | ||
return SshKey{}, errors.New("Must not specify organizationId") | ||
} | ||
organizationId, err := self.organizationId() | ||
if err != nil { | ||
return SshKey{}, nil | ||
} | ||
payload.OrganizationId = organizationId | ||
|
||
var result SshKey | ||
err = self.postJSON("/ssh-keys", payload, &result) | ||
if err != nil { | ||
return SshKey{}, err | ||
} | ||
return result, nil | ||
} | ||
|
||
func (self *ApiClient) SshKeyDelete(id string) error { | ||
return self.delete("/ssh-keys/" + id) | ||
} | ||
|
||
func (self *ApiClient) SshKeys() ([]SshKey, error) { | ||
organizationId, err := self.organizationId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
var result []SshKey | ||
params := url.Values{} | ||
params.Add("organizationId", organizationId) | ||
err = self.getJSON("/ssh-keys", params, &result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return result, err | ||
} |
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,73 @@ | ||
package env0tfprovider | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/env0/terraform-provider-env0/env0apiclient" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSshKey() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSshKeyRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "the name of the ssh key", | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "id"}, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "id of the ssh key", | ||
Optional: true, | ||
ExactlyOneOf: []string{"name", "id"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSshKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(*env0apiclient.ApiClient) | ||
|
||
name, nameSpecified := d.GetOk("name") | ||
var sshKey env0apiclient.SshKey | ||
if nameSpecified { | ||
sshKeys, err := apiClient.SshKeys() | ||
if err != nil { | ||
return diag.Errorf("Could not query ssh keys: %v", err) | ||
} | ||
for _, candidate := range sshKeys { | ||
if candidate.Name == name { | ||
sshKey = candidate | ||
} | ||
} | ||
if sshKey.Name == "" { | ||
return diag.Errorf("Could not find an env0 ssh key with name %s", name) | ||
} | ||
} else { | ||
id, idSpecified := d.GetOk("id") | ||
if !idSpecified { | ||
return diag.Errorf("At lease one of 'id', 'name' must be specified") | ||
} | ||
sshKeys, err := apiClient.SshKeys() | ||
if err != nil { | ||
return diag.Errorf("Could not query ssh keys: %v", err) | ||
} | ||
for _, candidate := range sshKeys { | ||
if candidate.Id == id.(string) { | ||
sshKey = candidate | ||
} | ||
} | ||
if sshKey.Name == "" { | ||
return diag.Errorf("Could not find an env0 ssh key with id %s", id) | ||
} | ||
} | ||
|
||
d.SetId(sshKey.Id) | ||
d.Set("name", sshKey.Name) | ||
|
||
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
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.