-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_cloud_project_user_s3_credential.go
162 lines (130 loc) · 4.3 KB
/
resource_cloud_project_user_s3_credential.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceCloudProjectUserS3Credential() *schema.Resource {
return &schema.Resource{
Create: resourceCloudProjectUserS3CredentialCreate,
Read: resourceCloudProjectUserS3CredentialRead,
Delete: resourceCloudProjectUserS3CredentialDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceCloudProjectUserS3CredentialImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
Description: "Service name of the resource representing the ID of the cloud project.",
},
"user_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The user ID",
},
"internal_user_id": {
Type: schema.TypeString,
Computed: true,
},
"access_key_id": {
Type: schema.TypeString,
Computed: true,
},
"secret_access_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
}
}
func resourceCloudProjectUserS3CredentialImportState(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
access := d.Id()
serviceName := ""
userId := ""
splitId := strings.SplitN(access, "/", 3)
if len(splitId) == 3 {
serviceName = splitId[0]
userId = splitId[1]
access = splitId[2]
}
if serviceName == "" || userId == "" || access == "" {
return nil, fmt.Errorf("Import Id is not service_name/user_id/access_key formatted")
}
d.SetId(access)
d.Set("service_name", serviceName)
d.Set("user_id", userId)
return []*schema.ResourceData{d}, nil
}
func resourceCloudProjectUserS3CredentialCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
userID := d.Get("user_id").(string)
s3Credential := &CloudProjectUserS3Credential{}
log.Printf("[DEBUG] Will create Public Cloud S3 AccessKey for user: %s from project: %s", userID, serviceName)
endpoint := fmt.Sprintf(
"/cloud/project/%s/user/%s/s3Credentials",
url.PathEscape(serviceName),
url.PathEscape(userID),
)
if err := config.OVHClient.Post(endpoint, nil, s3Credential); err != nil {
return fmt.Errorf("calling Post %s :\n\t %q", endpoint, err)
}
d.SetId(s3Credential.Access)
d.Set("secret_access_key", s3Credential.Secret)
for k, v := range s3Credential.ToMap() {
d.Set(k, v)
}
log.Printf("[DEBUG] Created the Public Cloud S3 AccessKey %s", s3Credential)
return resourceCloudProjectUserS3CredentialRead(d, meta)
}
func resourceCloudProjectUserS3CredentialRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
userID := d.Get("user_id").(string)
s3Credential := &CloudProjectUserS3Credential{}
log.Printf("[DEBUG] Will read the Public Cloud S3 AccessKey %s for user %s from project: %s", d.Id(), userID, serviceName)
endpoint := fmt.Sprintf(
"/cloud/project/%s/user/%s/s3Credentials/%s",
url.PathEscape(serviceName),
userID,
d.Id(),
)
if err := config.OVHClient.Get(endpoint, s3Credential); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
d.SetId(s3Credential.Access)
// set resource attributes
for k, v := range s3Credential.ToMap() {
d.Set(k, v)
}
log.Printf("[DEBUG] Read the Public Cloud S3 AccessKey %s", s3Credential)
return nil
}
func resourceCloudProjectUserS3CredentialDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
userID := d.Get("user_id").(string)
id := d.Id()
log.Printf("[DEBUG] Will delete the Public Cloud S3 AccessKey %s for user %s from project: %s", id, userID, serviceName)
endpoint := fmt.Sprintf(
"/cloud/project/%s/user/%s/s3Credentials/%s",
url.PathEscape(serviceName),
userID,
id,
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return fmt.Errorf("calling Delete %s:\n\t %q", endpoint, err)
}
log.Printf("[DEBUG] Deleted the Public Cloud S3 AccessKey %s for user %s from project %s", id, userID, serviceName)
d.SetId("")
return nil
}