-
Notifications
You must be signed in to change notification settings - Fork 90
/
resource_robot_account.go
134 lines (114 loc) · 2.92 KB
/
resource_robot_account.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
package provider
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
// var pathRobot string = "/projects"
func resourceRobotAccount() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Default: nil,
},
"actions": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
// Default: ["pull"],
ForceNew: true,
},
"token": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"robot_id": {
Type: schema.TypeString,
Computed: true,
},
},
Create: resourceRobotAccountCreate,
Read: resourceRobotAccountRead,
Update: resourceRobotAccountUpdate,
Delete: resourceRobotAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}
func checkProjectid(id string) (projecid string) {
path := "/projects/"
if strings.Contains(id, path) == false {
id = path + id
}
return id
}
func resourceRobotAccountCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
projectid := d.Get("project_id").(string)
url := projectid + "/robots"
body := client.RobotBody(d, projectid)
resp, headers, err := apiClient.SendRequest("POST", url, body, 201)
if err != nil {
return err
}
var jsonData models.RobotBodyRepones
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return err
}
id, err := client.GetID(headers)
if err != nil {
return err
}
d.SetId(id)
d.Set("token", jsonData.Token)
return resourceRobotAccountRead(d, m)
}
func resourceRobotAccountRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
resp, _, err := apiClient.SendRequest("GET", d.Id(), nil, 200)
if err != nil {
return err
}
var jsonData models.RobotBodyRepones
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Resource not found %s", d.Id())
}
d.Set("project_id", "/projects/"+strconv.Itoa(jsonData.ProjectID))
d.Set("robot_id", strconv.Itoa(jsonData.ID))
d.Set("name", strings.Replace(jsonData.Name, "robot$", "", -1))
d.Set("description", jsonData.Description)
return nil
}
func resourceRobotAccountUpdate(d *schema.ResourceData, m interface{}) error {
return resourceRobotAccountRead(d, m)
}
func resourceRobotAccountDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
_, _, err := apiClient.SendRequest("DELETE", d.Id(), nil, 200)
if err != nil {
return err
}
return nil
}