|
| 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 | +type Role struct { |
| 14 | + Name string `json:"name"` |
| 15 | + DisplayName string `json:"display-name"` |
| 16 | +} |
| 17 | + |
| 18 | +func userDataSource() *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, i interface{}) diag.Diagnostics { |
| 22 | + if idStr, ok := os.LookupEnv("CODER_USER_ID"); !ok { |
| 23 | + return diag.Errorf("missing user id") |
| 24 | + } else { |
| 25 | + rd.SetId(idStr) |
| 26 | + } |
| 27 | + |
| 28 | + if username, ok := os.LookupEnv("CODER_USER_USERNAME"); !ok { |
| 29 | + return diag.Errorf("missing user username") |
| 30 | + } else { |
| 31 | + _ = rd.Set("username", username) |
| 32 | + } |
| 33 | + |
| 34 | + if avatarURL, ok := os.LookupEnv("CODER_USER_AVATAR_URL"); !ok { |
| 35 | + return diag.Errorf("missing user avatar_url") |
| 36 | + } else { |
| 37 | + _ = rd.Set("avatar_url", avatarURL) |
| 38 | + } |
| 39 | + |
| 40 | + if fullname, ok := os.LookupEnv("CODER_USER_NAME"); !ok { |
| 41 | + _ = rd.Set("name", "default") // compat |
| 42 | + } else { |
| 43 | + _ = rd.Set("name", fullname) |
| 44 | + } |
| 45 | + |
| 46 | + if email, ok := os.LookupEnv("CODER_USER_EMAIL"); !ok { |
| 47 | + return diag.Errorf("missing user email") |
| 48 | + } else { |
| 49 | + _ = rd.Set("email", email) |
| 50 | + } |
| 51 | + |
| 52 | + if createdAt, ok := os.LookupEnv("CODER_USER_CREATED_AT"); !ok { |
| 53 | + return diag.Errorf("missing user created_at") |
| 54 | + } else { |
| 55 | + _ = rd.Set("created_at", createdAt) |
| 56 | + } |
| 57 | + |
| 58 | + if lastSeenAt, ok := os.LookupEnv("CODER_USER_LAST_SEEN_AT"); !ok { |
| 59 | + return diag.Errorf("missing user last_seen_at") |
| 60 | + } else { |
| 61 | + _ = rd.Set("last_seen_at", lastSeenAt) |
| 62 | + } |
| 63 | + |
| 64 | + if status, ok := os.LookupEnv("CODER_USER_STATUS"); !ok { |
| 65 | + return diag.Errorf("missing user status") |
| 66 | + } else { |
| 67 | + _ = rd.Set("status", status) |
| 68 | + } |
| 69 | + |
| 70 | + if loginType, ok := os.LookupEnv("CODER_USER_LOGIN_TYPE"); !ok { |
| 71 | + return diag.Errorf("missing user login_type") |
| 72 | + } else { |
| 73 | + _ = rd.Set("login_type", loginType) |
| 74 | + } |
| 75 | + |
| 76 | + if themePref, ok := os.LookupEnv("CODER_USER_THEME_PREFERENCE"); !ok { |
| 77 | + return diag.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 | + return diag.Errorf("missing user organization_ids") |
| 85 | + } |
| 86 | + var orgIDs []string |
| 87 | + if err := json.NewDecoder(strings.NewReader(orgIDsRaw)).Decode(&orgIDs); err != nil { |
| 88 | + return diag.Errorf("invalid user organization_ids: %s", err.Error()) |
| 89 | + } |
| 90 | + _ = rd.Set("organization_ids", orgIDs) |
| 91 | + |
| 92 | + if sshPubKey, ok := os.LookupEnv("CODER_USER_SSH_PUBLIC_KEY"); !ok { |
| 93 | + return diag.Errorf("missing user ssh_public_key") |
| 94 | + } else { |
| 95 | + _ = rd.Set("ssh_public_key", sshPubKey) |
| 96 | + } |
| 97 | + |
| 98 | + if sshPrivKey, ok := os.LookupEnv("CODER_USER_SSH_PRIVATE_KEY"); !ok { |
| 99 | + return diag.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 | + return diag.Errorf("missing user groups") |
| 107 | + } |
| 108 | + var groups []string |
| 109 | + if err := json.NewDecoder(strings.NewReader(groupsRaw)).Decode(&groups); err != nil { |
| 110 | + return diag.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 | + return diag.Errorf("missing user roles") |
| 118 | + } |
| 119 | + var roles []Role |
| 120 | + if err := json.NewDecoder(strings.NewReader(rolesRaw)).Decode(&roles); err != nil { |
| 121 | + return diag.Errorf("invalid user roles: %s", err.Error()) |
| 122 | + } else { |
| 123 | + _ = rd.Set("roles", roles) |
| 124 | + } |
| 125 | + |
| 126 | + return nil |
| 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 | +} |
0 commit comments