-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_zpa_ba_certificate.go
116 lines (100 loc) · 3.18 KB
/
resource_zpa_ba_certificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package zpa
import (
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
client "github.com/zscaler/zscaler-sdk-go/v2/zpa"
"github.com/zscaler/zscaler-sdk-go/v2/zpa/services/bacertificate"
)
func resourceBaCertificate() *schema.Resource {
return &schema.Resource{
Create: resourceBaCertificateCreate,
Read: resourceBaCertificateRead,
UpdateContext: resourceFuncNoOp,
Delete: resourceBaCertificateDelete,
Importer: nil,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier of the certificate",
},
"cert_blob": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The description of the certificate",
},
"certificate": {
Type: schema.TypeString,
Computed: true,
Description: "The certificate text in PEM format",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the certificate.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The description of the certificate",
},
"microtenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The unique identifier of the Microtenant",
},
},
}
}
func resourceBaCertificateCreate(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).bacertificate.WithMicroTenant(GetString(d.Get("microtenant_id")))
req := expandBaCertificate(d)
log.Printf("[INFO] Creating certificate with request\n%+v\n", req)
baCertificate, _, err := service.Create(req)
if err != nil {
return err
}
log.Printf("[INFO] Created certificate request. ID: %v\n", baCertificate)
d.SetId(baCertificate.ID)
return nil
}
func resourceBaCertificateRead(d *schema.ResourceData, m interface{}) error {
zClient := m.(*Client)
resp, _, err := zClient.bacertificate.Get(d.Id())
if err != nil {
if errResp, ok := err.(*client.ErrorResponse); ok && errResp.IsObjectNotFound() {
log.Printf("[WARN] Removing ba certificate %s from state because it no longer exists in ZPA", d.Id())
d.SetId("")
return nil
}
return err
}
log.Printf("[INFO] Getting ba certificate:\n%+v\n", resp)
d.SetId(resp.ID)
_ = d.Set("name", resp.Name)
_ = d.Set("description", resp.Description)
_ = d.Set("certificate", resp.Certificate)
_ = d.Set("microtenant_id", resp.MicrotenantID)
return nil
}
func resourceBaCertificateDelete(d *schema.ResourceData, m interface{}) error {
service := m.(*Client).bacertificate.WithMicroTenant(GetString(d.Get("microtenant_id")))
log.Printf("[INFO] Deleting certificate ID: %v\n", d.Id())
if _, err := service.Delete(d.Id()); err != nil {
return err
}
d.SetId("")
log.Printf("[INFO] certificate deleted")
return nil
}
func expandBaCertificate(d *schema.ResourceData) bacertificate.BaCertificate {
baCertificate := bacertificate.BaCertificate{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
CertBlob: d.Get("cert_blob").(string),
MicrotenantID: d.Get("microtenant_id").(string),
}
return baCertificate
}