/
user.go
128 lines (116 loc) · 3.57 KB
/
user.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
package iam
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)
func ResourceUser() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIamUserCreate,
ReadContext: resourceIamUserRead,
DeleteContext: resourceIamUserDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The description of the iam user",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of the creation of the iam user",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of the last update of the iam user",
},
"deletable": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether or not the iam user is editable",
},
"last_login_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of last login of the iam user",
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of the iam user",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of user invitation.",
},
"mfa": {
Type: schema.TypeBool,
Computed: true,
Description: "Whether or not the MFA is enabled",
},
"account_root_user_id": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the account root user associated with the iam user.",
},
"organization_id": account.OrganizationIDOptionalSchema(),
},
}
}
func resourceIamUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
user, err := api.CreateUser(&iam.CreateUserRequest{
OrganizationID: d.Get("organization_id").(string),
Email: d.Get("email").(string),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
d.SetId(user.ID)
return resourceIamUserRead(ctx, d, m)
}
func resourceIamUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
user, err := api.GetUser(&iam.GetUserRequest{
UserID: d.Id(),
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
_ = d.Set("email", user.Email)
_ = d.Set("created_at", types.FlattenTime(user.CreatedAt))
_ = d.Set("updated_at", types.FlattenTime(user.UpdatedAt))
_ = d.Set("organization_id", user.OrganizationID)
_ = d.Set("deletable", user.Deletable)
_ = d.Set("last_login_at", types.FlattenTime(user.LastLoginAt))
_ = d.Set("type", user.Type)
_ = d.Set("status", user.Status)
_ = d.Set("mfa", user.Mfa)
return nil
}
func resourceIamUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
err := api.DeleteUser(&iam.DeleteUserRequest{
UserID: d.Id(),
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return diag.FromErr(err)
}
return nil
}