|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = registerDataSource("gitlab_current_user", func() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: `The ` + "`gitlab_current_user`" + ` data source allows details of the current user (determined by ` + "`token`" + ` provider attribute) to be retrieved. |
| 16 | +
|
| 17 | +**Upstream API**: [GitLab GraphQL API docs](https://docs.gitlab.com/ee/api/graphql/reference/index.html#querycurrentuser)`, |
| 18 | + |
| 19 | + ReadContext: dataSourceGitlabCurrentUserRead, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "id": { |
| 22 | + Description: "ID of the user.", |
| 23 | + Type: schema.TypeString, |
| 24 | + Computed: true, |
| 25 | + }, |
| 26 | + "global_id": { |
| 27 | + Description: "Global ID of the user. This is in the form of a GraphQL globally unique ID.", |
| 28 | + Type: schema.TypeString, |
| 29 | + Computed: true, |
| 30 | + }, |
| 31 | + "username": { |
| 32 | + Description: "Username of the user. Unique within this instance of GitLab.", |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "name": { |
| 37 | + Description: "Human-readable name of the user. Returns **** if the user is a project bot and the requester does not have permission to view the project.", |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "bot": { |
| 42 | + Description: "Indicates if the user is a bot.", |
| 43 | + Type: schema.TypeBool, |
| 44 | + Computed: true, |
| 45 | + }, |
| 46 | + "group_count": { |
| 47 | + Description: "Group count for the user.", |
| 48 | + Type: schema.TypeInt, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "namespace_id": { |
| 52 | + Description: "Personal namespace of the user.", |
| 53 | + Type: schema.TypeString, |
| 54 | + Computed: true, |
| 55 | + }, |
| 56 | + "global_namespace_id": { |
| 57 | + Description: "Personal namespace of the user. This is in the form of a GraphQL globally unique ID.", |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + }, |
| 61 | + "public_email": { |
| 62 | + Description: "User’s public email.", |
| 63 | + Type: schema.TypeString, |
| 64 | + Computed: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | +}) |
| 69 | + |
| 70 | +func dataSourceGitlabCurrentUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 71 | + client := meta.(*gitlab.Client) |
| 72 | + |
| 73 | + query := GraphQLQuery{ |
| 74 | + `query {currentUser {name, bot, groupCount, id, namespace{id}, publicEmail, username}}`, |
| 75 | + } |
| 76 | + log.Printf("[DEBUG] executing GraphQL Query %s to retrieve current user", query.Query) |
| 77 | + |
| 78 | + var response CurrentUserResponse |
| 79 | + if _, err := SendGraphQLRequest(ctx, client, query, &response); err != nil { |
| 80 | + return diag.FromErr(err) |
| 81 | + } |
| 82 | + |
| 83 | + userID, err := extractIIDFromGlobalID(response.Data.CurrentUser.ID) |
| 84 | + if err != nil { |
| 85 | + return diag.FromErr(err) |
| 86 | + } |
| 87 | + |
| 88 | + namespaceID, err := extractIIDFromGlobalID(response.Data.CurrentUser.Namespace.ID) |
| 89 | + if err != nil { |
| 90 | + return diag.FromErr(err) |
| 91 | + } |
| 92 | + |
| 93 | + d.SetId(fmt.Sprintf("%d", userID)) |
| 94 | + d.Set("global_id", response.Data.CurrentUser.ID) |
| 95 | + d.Set("username", response.Data.CurrentUser.Username) |
| 96 | + d.Set("name", response.Data.CurrentUser.Name) |
| 97 | + d.Set("bot", response.Data.CurrentUser.Bot) |
| 98 | + d.Set("group_count", response.Data.CurrentUser.GroupCount) |
| 99 | + d.Set("namespace_id", fmt.Sprintf("%d", namespaceID)) |
| 100 | + d.Set("global_namespace_id", response.Data.CurrentUser.Namespace.ID) |
| 101 | + d.Set("public_email", response.Data.CurrentUser.PublicEmail) |
| 102 | + |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// Struct representing current user based on the input API token |
| 107 | +type CurrentUserResponse struct { |
| 108 | + Data struct { |
| 109 | + CurrentUser GraphQLUser `json:"currentUser"` |
| 110 | + } `json:"data"` |
| 111 | +} |
| 112 | + |
| 113 | +type GraphQLUser struct { |
| 114 | + Name string `json:"name"` |
| 115 | + Bot bool `json:"bot"` |
| 116 | + GroupCount int `json:"groupCount"` |
| 117 | + ID string `json:"id"` // This is purposefully a string, as in some APIs it comes back as a globally unique ID |
| 118 | + Namespace struct { |
| 119 | + ID string `json:"id"` |
| 120 | + } `json:"namespace"` |
| 121 | + PublicEmail string `json:"publicEmail"` |
| 122 | + Username string `json:"username"` |
| 123 | +} |
0 commit comments