Skip to content

Commit 218f618

Browse files
authored
Merge pull request #51 from cloud-ca/development
v1.2.0
2 parents 42992c4 + 29933ec commit 218f618

File tree

7 files changed

+256
-22
lines changed

7 files changed

+256
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Terraform provider for cloud.ca
44

5-
Tested with Terraform version : 0.11.1
5+
Tested with Terraform version : 0.11.5
66

77
# Installation
88

cloudca/README.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ resource "cloudca_network" "my_network" {
7373
name = "test-network"
7474
description = "This is a test network"
7575
vpc_id = "8b46e2d1-bbc4-4fad-b3bd-1b25fcba4cec"
76-
network_offering = "Standard Network"
77-
network_acl_id = "7d428416-263d-47cd-9270-2cdbdf222f57"
76+
network_offering = "Standard Tier"
77+
network_acl = "7d428416-263d-47cd-9270-2cdbdf222f57"
7878
}
7979
```
8080
### Argument Reference
@@ -84,7 +84,7 @@ The following arguments are supported:
8484
- description - (Required) Description of the network
8585
- vpc_id - (Required) The ID of the vpc where the network should be created
8686
- network_offering - (Required) The name of the network offering to use for the network
87-
- network_acl_id - (Required) The id of the network ACL to use for the network
87+
- network_acl - (Required) The id or name of the network ACL to use for the network
8888

8989
### Attribute Reference
9090
- id - ID of network.
@@ -157,7 +157,7 @@ resource "cloudca_instance" "my_instance" {
157157
environment_id = "4cad744d-bf1f-423d-887b-bbb34f4d1b5b"
158158
name = "test-instance"
159159
network_id = "672016ef-05ee-4e88-b68f-ac9cc462300b"
160-
template = "CentOS 6.7 base (64bit)"
160+
template = "Ubuntu 16.04.03 HVM"
161161
compute_offering = "1vCPU.512MB"
162162
ssh_key_name = "my_ssh_key"
163163
root_volume_size_in_gb = 100
@@ -320,4 +320,24 @@ resource "cloudca_load_balancer_rule" "lbr" {
320320
- stickiness_params - (Optional) The additional parameters required for each stickiness method. See (TODO ADD LINK here) for more information
321321

322322
### Attribute reference
323+
323324
- id - the load balancer rule ID
325+
326+
## cloudca_ssh_key
327+
328+
Adds an SSH key to the environment so that it can be associated with instances.
329+
330+
### Example usage
331+
332+
```hcl
333+
resource "cloudca_ssh_key" "dev_ssh_key" {
334+
environment_id = "4cad744d-bf1f-423d-887b-bbb34f4d1b5b"
335+
name = "my-ssh-key"
336+
public_key = "my_public_key_data"
337+
}
338+
```
339+
340+
### Argument reference
341+
- environment_id - (Required) ID of environment
342+
- name - (Required) The name of the SSH key to add
343+
- public_key - (Required) The public key data

cloudca/resource_cloudca.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func GetCloudCAResourceMap() map[string]*schema.Resource {
2424
"cloudca_network_acl": resourceCloudcaNetworkAcl(),
2525
"cloudca_network_acl_rule": resourceCloudcaNetworkAclRule(),
2626
"cloudca_static_nat": resourceCloudcaStaticNat(),
27+
"cloudca_ssh_key": resourceCloudcaSSHKey(),
2728
}
2829
}
2930

cloudca/resource_cloudca_network.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ func resourceCloudcaNetwork() *schema.Resource {
5353
ForceNew: true,
5454
Description: `The network offering name or id (e.g. "Standard Network" or "Load Balanced Network")`,
5555
},
56-
"network_acl_id": {
56+
"network_acl": {
5757
Type: schema.TypeString,
5858
Required: true,
59-
Description: "Id of the network ACL",
59+
Description: "Name or id of the network ACL",
6060
},
6161
"cidr": {
6262
Type: schema.TypeString,
@@ -77,16 +77,21 @@ func resourceCloudcaNetworkCreate(d *schema.ResourceData, meta interface{}) erro
7777
return nerr
7878
}
7979

80+
aclID, nerr := retrieveNetworkAclID(&ccaResources, d.Get("network_acl").(string), d.Get("vpc_id").(string))
81+
if nerr != nil {
82+
return nerr
83+
}
84+
8085
networkToCreate := cloudca.Network{
8186
Name: d.Get("name").(string),
8287
Description: d.Get("description").(string),
8388
VpcId: d.Get("vpc_id").(string),
8489
NetworkOfferingId: networkOfferingId,
85-
NetworkAclId: d.Get("network_acl_id").(string),
90+
NetworkAclId: aclID,
8691
}
8792
options := map[string]string{}
88-
if orgId, ok := d.GetOk("organization_code"); ok {
89-
options["org_id"] = orgId.(string)
93+
if orgID, ok := d.GetOk("organization_code"); ok {
94+
options["org_id"] = orgID.(string)
9095
}
9196
newNetwork, err := ccaResources.Networks.Create(networkToCreate, options)
9297
if err != nil {
@@ -131,7 +136,7 @@ func resourceCloudcaNetworkRead(d *schema.ResourceData, meta interface{}) error
131136
d.Set("description", network.Description)
132137
setValueOrID(d, "network_offering", offering.Name, network.NetworkOfferingId)
133138
d.Set("vpc_id", network.VpcId)
134-
d.Set("network_acl_id", network.NetworkAclId)
139+
setValueOrID(d, "network_acl", network.NetworkAclName, network.NetworkAclId)
135140
d.Set("cidr", network.Cidr)
136141
return nil
137142
}
@@ -153,8 +158,12 @@ func resourceCloudcaNetworkUpdate(d *schema.ResourceData, meta interface{}) erro
153158
}
154159
}
155160

156-
if d.HasChange("network_acl_id") {
157-
_, aclErr := ccaResources.Networks.ChangeAcl(d.Id(), d.Get("network_acl_id").(string))
161+
if d.HasChange("network_acl") {
162+
aclID, err := retrieveNetworkAclID(&ccaResources, d.Get("network_acl").(string), d.Get("vpc_id").(string))
163+
if err != nil {
164+
return err
165+
}
166+
_, aclErr := ccaResources.Networks.ChangeAcl(d.Id(), aclID)
158167
if aclErr != nil {
159168
return aclErr
160169
}
@@ -201,3 +210,19 @@ func retrieveNetworkOfferingId(ccaRes *cloudca.Resources, name string) (id strin
201210
}
202211
return "", fmt.Errorf("Network offering with name %s not found", name)
203212
}
213+
214+
func retrieveNetworkAclID(ccaRes *cloudca.Resources, name, vpcID string) (id string, err error) {
215+
if isID(name) {
216+
return name, nil
217+
}
218+
acls, err := ccaRes.NetworkAcls.ListByVpcId(vpcID)
219+
if err != nil {
220+
return "", err
221+
}
222+
for _, acl := range acls {
223+
if strings.EqualFold(acl.Name, name) {
224+
return acl.Id, nil
225+
}
226+
}
227+
return "", fmt.Errorf("Network ACL with name %s not found", name)
228+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package cloudca
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/cloud-ca/go-cloudca"
8+
"github.com/cloud-ca/go-cloudca/api"
9+
"github.com/cloud-ca/go-cloudca/services/cloudca"
10+
"github.com/hashicorp/terraform/helper/schema"
11+
)
12+
13+
func resourceCloudcaSSHKey() *schema.Resource {
14+
return &schema.Resource{
15+
Create: createSSHKey,
16+
Read: readSSHKey,
17+
Delete: deleteSSHKey,
18+
19+
Schema: map[string]*schema.Schema{
20+
"environment_id": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
ForceNew: true,
24+
Description: "ID of environment where the SSH key should be created",
25+
},
26+
"name": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
ForceNew: true,
30+
Description: "Name of the SSH Key",
31+
},
32+
"public_key": {
33+
Type: schema.TypeString,
34+
Required: true,
35+
ForceNew: true,
36+
},
37+
},
38+
}
39+
}
40+
41+
func createSSHKey(d *schema.ResourceData, meta interface{}) error {
42+
ccaResources, rerr := getResourcesForEnvironmentId(meta.(*cca.CcaClient), d.Get("environment_id").(string))
43+
44+
if rerr != nil {
45+
return rerr
46+
}
47+
name := d.Get("name").(string)
48+
publicKey := d.Get("public_key").(string)
49+
50+
sk := cloudca.SSHKey{
51+
Name: name,
52+
PublicKey: publicKey,
53+
}
54+
newSk, err := ccaResources.SSHKeys.Create(sk)
55+
if err != nil {
56+
return fmt.Errorf("Error creating new SSH key %s", err)
57+
}
58+
d.SetId(newSk.ID)
59+
return readSSHKey(d, meta)
60+
}
61+
62+
func readSSHKey(d *schema.ResourceData, meta interface{}) error {
63+
ccaResources, rerr := getResourcesForEnvironmentId(meta.(*cca.CcaClient), d.Get("environment_id").(string))
64+
65+
if rerr != nil {
66+
return rerr
67+
}
68+
sk, err := ccaResources.SSHKeys.Get(d.Id())
69+
if err != nil {
70+
if ccaError, ok := err.(api.CcaErrorResponse); ok {
71+
if ccaError.StatusCode == 404 {
72+
log.Printf("SSH key with id='%s' was not found", d.Id())
73+
d.SetId("")
74+
return nil
75+
}
76+
}
77+
return err
78+
}
79+
d.Set("name", sk.Name)
80+
return nil
81+
}
82+
83+
func deleteSSHKey(d *schema.ResourceData, meta interface{}) error {
84+
ccaResources, rerr := getResourcesForEnvironmentId(meta.(*cca.CcaClient), d.Get("environment_id").(string))
85+
86+
if rerr != nil {
87+
return rerr
88+
}
89+
90+
if _, err := ccaResources.SSHKeys.Delete(d.Id()); err != nil {
91+
if ccaError, ok := err.(api.CcaErrorResponse); ok {
92+
if ccaError.StatusCode == 404 {
93+
fmt.Printf("SSH key %s not found", d.Id())
94+
d.SetId("")
95+
return nil
96+
}
97+
}
98+
return err
99+
}
100+
101+
return nil
102+
}

0 commit comments

Comments
 (0)