|
| 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_NAME"); !ok { |
| 29 | + return diag.Errorf("missing user username") |
| 30 | + } else { |
| 31 | + _ = rd.Set("name", username) |
| 32 | + } |
| 33 | + |
| 34 | + if fullname, ok := os.LookupEnv("CODER_USER_FULL_NAME"); !ok { |
| 35 | + _ = rd.Set("name", "default") // compat |
| 36 | + } else { |
| 37 | + _ = rd.Set("full_name", fullname) |
| 38 | + } |
| 39 | + |
| 40 | + if email, ok := os.LookupEnv("CODER_USER_EMAIL"); !ok { |
| 41 | + return diag.Errorf("missing user email") |
| 42 | + } else { |
| 43 | + _ = rd.Set("email", email) |
| 44 | + } |
| 45 | + |
| 46 | + if sshPubKey, ok := os.LookupEnv("CODER_USER_SSH_PUBLIC_KEY"); !ok { |
| 47 | + return diag.Errorf("missing user ssh_public_key") |
| 48 | + } else { |
| 49 | + _ = rd.Set("ssh_public_key", sshPubKey) |
| 50 | + } |
| 51 | + |
| 52 | + if sshPrivKey, ok := os.LookupEnv("CODER_USER_SSH_PRIVATE_KEY"); !ok { |
| 53 | + return diag.Errorf("missing user ssh_private_key") |
| 54 | + } else { |
| 55 | + _ = rd.Set("ssh_private_key", sshPrivKey) |
| 56 | + } |
| 57 | + |
| 58 | + groupsRaw, ok := os.LookupEnv("CODER_USER_GROUPS") |
| 59 | + if !ok { |
| 60 | + return diag.Errorf("missing user groups") |
| 61 | + } |
| 62 | + var groups []string |
| 63 | + if err := json.NewDecoder(strings.NewReader(groupsRaw)).Decode(&groups); err != nil { |
| 64 | + return diag.Errorf("invalid user groups: %s", err.Error()) |
| 65 | + } else { |
| 66 | + _ = rd.Set("groups", groups) |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | + }, |
| 71 | + Schema: map[string]*schema.Schema{ |
| 72 | + "id": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + Description: "The UUID of the user.", |
| 76 | + }, |
| 77 | + "name": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + Description: "The username of the user.", |
| 81 | + }, |
| 82 | + "full_name": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "The full name of the user.", |
| 86 | + }, |
| 87 | + "email": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "The email address of the user.", |
| 91 | + }, |
| 92 | + "ssh_public_key": { |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + Description: "The user's generated SSH public key.", |
| 96 | + }, |
| 97 | + "ssh_private_key": { |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + Description: "The user's generated SSH private key.", |
| 101 | + Sensitive: true, |
| 102 | + }, |
| 103 | + "groups": { |
| 104 | + Type: schema.TypeList, |
| 105 | + Elem: &schema.Schema{ |
| 106 | + Type: schema.TypeString, |
| 107 | + }, |
| 108 | + Computed: true, |
| 109 | + Description: "The groups of which the user is a member.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
0 commit comments