-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_source_appgate_appliance_seed.go
109 lines (98 loc) · 3 KB
/
data_source_appgate_appliance_seed.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
package appgate
import (
"context"
b64 "encoding/base64"
"encoding/json"
"fmt"
"github.com/appgate/sdp-api-client-go/api/v15/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceAppgateApplianceSeed() *schema.Resource {
return &schema.Resource{
Read: dataSourceAppgateApplianceSeedRead,
Schema: map[string]*schema.Schema{
"appliance_id": {
Type: schema.TypeString,
Required: true,
},
"provide_cloud_ssh_key": {
Type: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"ssh_key", "password"},
},
"ssh_key": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"provide_cloud_ssh_key", "password"},
},
"latest_version": {
Type: schema.TypeBool,
Optional: true,
},
"password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
ConflictsWith: []string{"provide_cloud_ssh_key", "provide_cloud_ssh_key"},
},
"seed_file": {
Type: schema.TypeString,
Description: "Seed file (json) generated from appliance used in remote-exec.",
Computed: true,
},
},
}
}
func dataSourceAppgateApplianceSeedRead(d *schema.ResourceData, meta interface{}) error {
token := meta.(*Client).Token
api := meta.(*Client).API.AppliancesApi
ctx := context.TODO()
applianceID, iok := d.GetOk("appliance_id")
if !iok {
return fmt.Errorf("please provide one of appliance_id attribute")
}
request := api.AppliancesIdGet(ctx, applianceID.(string))
appliance, _, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
return fmt.Errorf("Failed to read Appliance, %+v", err)
}
if ok, _ := appliance.GetActivatedOk(); *ok {
d.Set("seed_file", "")
return fmt.Errorf("Appliance is already activated, %+v", err)
}
exportRequest := api.AppliancesIdExportPost(ctx, appliance.Id)
password, passwordOk := d.GetOk("password")
sshKey, sshOk := d.GetOk("ssh_key")
cloudKey, cloudOk := d.GetOk("provide_cloud_ssh_key")
sshConfig := openapi.NewSSHConfig()
if passwordOk {
sshConfig.Password = openapi.PtrString(password.(string))
d.Set("password", password.(string))
}
if sshOk {
sshConfig.SshKey = openapi.PtrString(sshKey.(string))
d.Set("ssh_key", sshKey.(string))
}
if cloudOk {
sshConfig.ProvideCloudSSHKey = openapi.PtrBool(cloudKey.(bool))
d.Set("provide_cloud_ssh_key", true)
}
if _, lvOk := d.GetOk("latest_version"); lvOk {
exportRequest.LatestVersion(true)
d.Set("latest_version", true)
}
exportRequest = exportRequest.SSHConfig(*sshConfig)
seedmap, _, err := exportRequest.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not export appliance %+v", prettyPrintAPIError(err))
}
encodedSeed, err := json.Marshal(seedmap)
if err != nil {
return fmt.Errorf("Could not parse json seed file: %+v", err)
}
d.SetId(applianceID.(string))
d.Set("appliance_id", appliance.Id)
d.Set("seed_file", b64.StdEncoding.EncodeToString([]byte(encodedSeed)))
return nil
}