-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_license.go
95 lines (81 loc) · 2.5 KB
/
resource_appgate_license.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
package appgate
import (
"context"
"fmt"
"log"
"time"
"github.com/appgate/sdp-api-client-go/api/v16/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAppgateLicense() *schema.Resource {
return &schema.Resource{
Create: resourceAppgateLicenseCreate,
Read: resourceAppgateLicenseRead,
Delete: resourceAppgateLicenseDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"license": {
Type: schema.TypeString,
Description: "The license file contents for this Controller (with the matching request code).",
ForceNew: true,
Sensitive: true,
Required: true,
},
},
}
}
func resourceAppgateLicenseCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Creating license")
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.LicenseApi
args := openapi.NewLicenseImportWithDefaults()
args.SetLicense(d.Get("license").(string))
if _, _, err := api.LicensePost(context.TODO()).LicenseImport(*args).Authorization(token).Execute(); err != nil {
return fmt.Errorf("Could not create license %+v", prettyPrintAPIError(err))
}
return resourceAppgateLicenseRead(d, meta)
}
func getLicenseIdentifier(license openapi.LicenseDetails) string {
return license.GetRequestCode()
}
func resourceAppgateLicenseRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading license")
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.LicenseApi
ctx := context.TODO()
request := api.LicenseGet(ctx)
license, _, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
return fmt.Errorf("Failed to read license, %+v", err)
}
d.SetId(getLicenseIdentifier(license))
return nil
}
func resourceAppgateLicenseDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Delete license")
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.LicenseApi
if _, err := api.LicenseDelete(context.TODO()).Authorization(token).Execute(); err != nil {
return fmt.Errorf("Could not delete license %+v", prettyPrintAPIError(err))
}
d.SetId("")
return nil
}