-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_source_appgate_appliance_seed.go
132 lines (119 loc) · 3.53 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package appgate
import (
"context"
b64 "encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/appgate/sdp-api-client-go/api/v19/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,
},
"activated": {
Type: schema.TypeBool,
Computed: 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.",
Sensitive: true,
Computed: true,
},
},
}
}
func dataSourceAppgateApplianceSeedRead(d *schema.ResourceData, meta interface{}) error {
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.AppliancesApi
currentVersion := meta.(*Client).ApplianceVersion
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, res, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
if res != nil && res.StatusCode == http.StatusNotFound {
return nil
}
return fmt.Errorf("Failed to read Appliance, %w", err)
}
d.SetId(applianceID.(string))
d.Set("appliance_id", appliance.Id)
d.Set("activated", appliance.GetActivated())
if ok, _ := appliance.GetActivatedOk(); *ok {
d.Set("seed_file", "")
log.Printf("[DEBUG] Appliance is already seeded")
return nil
}
exportRequest := api.AppliancesIdExportPost(ctx, appliance.GetId())
password, passwordOk := d.GetOk("password")
sshKey, sshOk := d.GetOk("ssh_key")
cloudKey, cloudOk := d.GetOk("provide_cloud_ssh_key")
sshConfig := openapi.NewSSHConfig()
// AllowCustomization and ValidityDays is only available in >= 5.5
if currentVersion.LessThan(Appliance55Version) {
sshConfig.AllowCustomization = nil
sshConfig.ValidityDays = nil
}
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 = 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 %w", prettyPrintAPIError(err))
}
encodedSeed, err := json.Marshal(seedmap)
if err != nil {
return fmt.Errorf("Could not parse json seed file: %w", err)
}
d.Set("seed_file", b64.StdEncoding.EncodeToString([]byte(encodedSeed)))
return nil
}