|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"encoding/json" |
| 6 | +"os" |
| 7 | +"strings" |
| 8 | + |
| 9 | +"github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | +"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | +) |
| 12 | + |
| 13 | +typeRolestruct { |
| 14 | +Namestring`json:"name"` |
| 15 | +DisplayNamestring`json:"display-name"` |
| 16 | +} |
| 17 | + |
| 18 | +funcuserDataSource()*schema.Resource { |
| 19 | +return&schema.Resource{ |
| 20 | +Description:"Use this data source to fetch information about a user.", |
| 21 | +ReadContext:func(ctx context.Context,rd*schema.ResourceData,iinterface{}) diag.Diagnostics { |
| 22 | +ifidStr,ok:=os.LookupEnv("CODER_USER_ID");!ok { |
| 23 | +returndiag.Errorf("missing user id") |
| 24 | +}else { |
| 25 | +rd.SetId(idStr) |
| 26 | +} |
| 27 | + |
| 28 | +ifusername,ok:=os.LookupEnv("CODER_USER_USERNAME");!ok { |
| 29 | +returndiag.Errorf("missing user username") |
| 30 | +}else { |
| 31 | +_=rd.Set("username",username) |
| 32 | +} |
| 33 | + |
| 34 | +ifavatarURL,ok:=os.LookupEnv("CODER_USER_AVATAR_URL");!ok { |
| 35 | +returndiag.Errorf("missing user avatar_url") |
| 36 | +}else { |
| 37 | +_=rd.Set("avatar_url",avatarURL) |
| 38 | +} |
| 39 | + |
| 40 | +iffullname,ok:=os.LookupEnv("CODER_USER_NAME");!ok { |
| 41 | +_=rd.Set("name","default")// compat |
| 42 | +}else { |
| 43 | +_=rd.Set("name",fullname) |
| 44 | +} |
| 45 | + |
| 46 | +ifemail,ok:=os.LookupEnv("CODER_USER_EMAIL");!ok { |
| 47 | +returndiag.Errorf("missing user email") |
| 48 | +}else { |
| 49 | +_=rd.Set("email",email) |
| 50 | +} |
| 51 | + |
| 52 | +ifcreatedAt,ok:=os.LookupEnv("CODER_USER_CREATED_AT");!ok { |
| 53 | +returndiag.Errorf("missing user created_at") |
| 54 | +}else { |
| 55 | +_=rd.Set("created_at",createdAt) |
| 56 | +} |
| 57 | + |
| 58 | +iflastSeenAt,ok:=os.LookupEnv("CODER_USER_LAST_SEEN_AT");!ok { |
| 59 | +returndiag.Errorf("missing user last_seen_at") |
| 60 | +}else { |
| 61 | +_=rd.Set("last_seen_at",lastSeenAt) |
| 62 | +} |
| 63 | + |
| 64 | +ifstatus,ok:=os.LookupEnv("CODER_USER_STATUS");!ok { |
| 65 | +returndiag.Errorf("missing user status") |
| 66 | +}else { |
| 67 | +_=rd.Set("status",status) |
| 68 | +} |
| 69 | + |
| 70 | +ifloginType,ok:=os.LookupEnv("CODER_USER_LOGIN_TYPE");!ok { |
| 71 | +returndiag.Errorf("missing user login_type") |
| 72 | +}else { |
| 73 | +_=rd.Set("login_type",loginType) |
| 74 | +} |
| 75 | + |
| 76 | +ifthemePref,ok:=os.LookupEnv("CODER_USER_THEME_PREFERENCE");!ok { |
| 77 | +returndiag.Errorf("missing user theme_preference") |
| 78 | +}else { |
| 79 | +_=rd.Set("theme_preference",themePref) |
| 80 | +} |
| 81 | + |
| 82 | +orgIDsRaw,ok:=os.LookupEnv("CODER_USER_ORGANIZATION_IDS") |
| 83 | +if!ok { |
| 84 | +returndiag.Errorf("missing user organization_ids") |
| 85 | +} |
| 86 | +varorgIDs []string |
| 87 | +iferr:=json.NewDecoder(strings.NewReader(orgIDsRaw)).Decode(&orgIDs);err!=nil { |
| 88 | +returndiag.Errorf("invalid user organization_ids: %s",err.Error()) |
| 89 | +} |
| 90 | +_=rd.Set("organization_ids",orgIDs) |
| 91 | + |
| 92 | +ifsshPubKey,ok:=os.LookupEnv("CODER_USER_SSH_PUBLIC_KEY");!ok { |
| 93 | +returndiag.Errorf("missing user ssh_public_key") |
| 94 | +}else { |
| 95 | +_=rd.Set("ssh_public_key",sshPubKey) |
| 96 | +} |
| 97 | + |
| 98 | +ifsshPrivKey,ok:=os.LookupEnv("CODER_USER_SSH_PRIVATE_KEY");!ok { |
| 99 | +returndiag.Errorf("missing user ssh_private_key") |
| 100 | +}else { |
| 101 | +_=rd.Set("ssh_private_key",sshPrivKey) |
| 102 | +} |
| 103 | + |
| 104 | +groupsRaw,ok:=os.LookupEnv("CODER_USER_GROUPS") |
| 105 | +if!ok { |
| 106 | +returndiag.Errorf("missing user groups") |
| 107 | +} |
| 108 | +vargroups []string |
| 109 | +iferr:=json.NewDecoder(strings.NewReader(groupsRaw)).Decode(&groups);err!=nil { |
| 110 | +returndiag.Errorf("invalid user groups: %s",err.Error()) |
| 111 | +}else { |
| 112 | +_=rd.Set("groups",groups) |
| 113 | +} |
| 114 | + |
| 115 | +rolesRaw,ok:=os.LookupEnv("CODER_USER_ROLES") |
| 116 | +if!ok { |
| 117 | +returndiag.Errorf("missing user roles") |
| 118 | +} |
| 119 | +varroles []Role |
| 120 | +iferr:=json.NewDecoder(strings.NewReader(rolesRaw)).Decode(&roles);err!=nil { |
| 121 | +returndiag.Errorf("invalid user roles: %s",err.Error()) |
| 122 | +}else { |
| 123 | +_=rd.Set("roles",roles) |
| 124 | +} |
| 125 | + |
| 126 | +returnnil |
| 127 | +}, |
| 128 | +Schema:map[string]*schema.Schema{ |
| 129 | +"id": { |
| 130 | +Type:schema.TypeString, |
| 131 | +Computed:true, |
| 132 | +Description:"The UUID of the user.", |
| 133 | +}, |
| 134 | +"username": { |
| 135 | +Type:schema.TypeString, |
| 136 | +Computed:true, |
| 137 | +Description:"The username of the user.", |
| 138 | +}, |
| 139 | +"avatar_url": { |
| 140 | +Type:schema.TypeString, |
| 141 | +Computed:true, |
| 142 | +Description:"The URL of the user's avatar.", |
| 143 | +}, |
| 144 | +"name": { |
| 145 | +Type:schema.TypeString, |
| 146 | +Computed:true, |
| 147 | +Description:"The full name of the user.", |
| 148 | +}, |
| 149 | +"email": { |
| 150 | +Type:schema.TypeString, |
| 151 | +Computed:true, |
| 152 | +Description:"The email address of the user.", |
| 153 | +}, |
| 154 | +"created_at": { |
| 155 | +Type:schema.TypeString, |
| 156 | +Computed:true, |
| 157 | +Description:"The time the user was created in RFC3339 format.", |
| 158 | +}, |
| 159 | +"last_seen_at": { |
| 160 | +Type:schema.TypeString, |
| 161 | +Computed:true, |
| 162 | +Description:"The time the user was last active in RFC3339 format.", |
| 163 | +}, |
| 164 | +"status": { |
| 165 | +Type:schema.TypeString, |
| 166 | +Computed:true, |
| 167 | +Description:"The status of the user.", |
| 168 | +}, |
| 169 | +"login_type": { |
| 170 | +Type:schema.TypeString, |
| 171 | +Computed:true, |
| 172 | +Description:"The user's login type.", |
| 173 | +}, |
| 174 | +"theme_preference": { |
| 175 | +Type:schema.TypeString, |
| 176 | +Computed:true, |
| 177 | +Description:"The user's theme preference.", |
| 178 | +}, |
| 179 | +"organization_ids": { |
| 180 | +Type:schema.TypeList, |
| 181 | +Elem:&schema.Schema{ |
| 182 | +Type:schema.TypeString, |
| 183 | +}, |
| 184 | +Computed:true, |
| 185 | +Description:"The organization IDs of which the user is a member.", |
| 186 | +}, |
| 187 | +"ssh_public_key": { |
| 188 | +Type:schema.TypeString, |
| 189 | +Computed:true, |
| 190 | +Description:"The user's generated SSH public key.", |
| 191 | +}, |
| 192 | +"ssh_private_key": { |
| 193 | +Type:schema.TypeString, |
| 194 | +Computed:true, |
| 195 | +Description:"The user's generated SSH private key.", |
| 196 | +Sensitive:true, |
| 197 | +}, |
| 198 | +"groups": { |
| 199 | +Type:schema.TypeList, |
| 200 | +Elem:&schema.Schema{ |
| 201 | +Type:schema.TypeString, |
| 202 | +}, |
| 203 | +Computed:true, |
| 204 | +Description:"The groups of which the user is a member.", |
| 205 | +}, |
| 206 | +"roles": { |
| 207 | +Type:schema.TypeList, |
| 208 | +Elem:&schema.Resource{ |
| 209 | +Schema:map[string]*schema.Schema{ |
| 210 | +"name": { |
| 211 | +Type:schema.TypeString, |
| 212 | +Computed:true, |
| 213 | +Description:"The internal name of the role.", |
| 214 | +}, |
| 215 | +"display_name": { |
| 216 | +Type:schema.TypeString, |
| 217 | +Computed:true, |
| 218 | +Description:"The display name of the role in the UI.", |
| 219 | +}, |
| 220 | +}, |
| 221 | +}, |
| 222 | +Computed:true, |
| 223 | +Description:"The roles assigned to the user.", |
| 224 | +}, |
| 225 | +}, |
| 226 | +} |
| 227 | +} |