|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + gitlab "github.com/xanzy/go-gitlab" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = registerDataSource("gitlab_instance_deploy_keys", func() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: `The ` + "`gitlab_instance_deploy_keys`" + ` data source allows to retrieve a list of deploy keys for a GitLab instance. |
| 17 | +
|
| 18 | +-> This data source requires administration privileges. |
| 19 | +
|
| 20 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/deploy_keys.html#list-all-deploy-keys)`, |
| 21 | + |
| 22 | + ReadContext: dataSourceGitlabInstanceDeployKeysRead, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "public": { |
| 25 | + Description: "Only return deploy keys that are public.", |
| 26 | + Type: schema.TypeBool, |
| 27 | + Optional: true, |
| 28 | + }, |
| 29 | + "deploy_keys": { |
| 30 | + Description: "The list of all deploy keys across all projects of the GitLab instance.", |
| 31 | + Type: schema.TypeList, |
| 32 | + Computed: true, |
| 33 | + Elem: &schema.Resource{ |
| 34 | + Schema: map[string]*schema.Schema{ |
| 35 | + "id": { |
| 36 | + Description: "The ID of the deploy key.", |
| 37 | + Type: schema.TypeInt, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "title": { |
| 41 | + Description: "The title of the deploy key.", |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "created_at": { |
| 46 | + Description: "The creation date of the deploy key. In RFC3339 format.", |
| 47 | + Type: schema.TypeString, |
| 48 | + Computed: true, |
| 49 | + }, |
| 50 | + "key": { |
| 51 | + Description: "The deploy key.", |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "fingerprint": { |
| 56 | + Description: "The fingerprint of the deploy key.", |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "projects_with_write_access": { |
| 61 | + Description: "The list of projects that the deploy key has write access to.", |
| 62 | + Type: schema.TypeList, |
| 63 | + Computed: true, |
| 64 | + Elem: &schema.Resource{ |
| 65 | + Schema: map[string]*schema.Schema{ |
| 66 | + "id": { |
| 67 | + Description: "The ID of the project.", |
| 68 | + Type: schema.TypeInt, |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + "description": { |
| 72 | + Description: "The description of the project.", |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "name": { |
| 77 | + Description: "The name of the project.", |
| 78 | + Type: schema.TypeString, |
| 79 | + Computed: true, |
| 80 | + }, |
| 81 | + "name_with_namespace": { |
| 82 | + Description: "The name of the project with namespace.", |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "path": { |
| 87 | + Description: "The path of the project.", |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "path_with_namespace": { |
| 92 | + Description: "The path of the project with namespace.", |
| 93 | + Type: schema.TypeString, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "created_at": { |
| 97 | + Description: "The creation date of the project. In RFC3339 format.", |
| 98 | + Type: schema.TypeString, |
| 99 | + Computed: true, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + } |
| 109 | +}) |
| 110 | + |
| 111 | +func dataSourceGitlabInstanceDeployKeysRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 112 | + client := meta.(*gitlab.Client) |
| 113 | + |
| 114 | + // Get group memberships |
| 115 | + options := &gitlab.ListInstanceDeployKeysOptions{ |
| 116 | + ListOptions: gitlab.ListOptions{ |
| 117 | + PerPage: 20, |
| 118 | + Page: 1, |
| 119 | + }, |
| 120 | + Public: gitlab.Bool(d.Get("public").(bool)), |
| 121 | + } |
| 122 | + |
| 123 | + log.Printf("[INFO] Reading Instance Deploy Keys, with: %v", options) |
| 124 | + |
| 125 | + var instanceDeployKeys []*gitlab.InstanceDeployKey |
| 126 | + for options.Page != 0 { |
| 127 | + paginatedInstancedeployKeys, resp, err := client.DeployKeys.ListAllDeployKeys(options, gitlab.WithContext(ctx)) |
| 128 | + if err != nil { |
| 129 | + return diag.FromErr(err) |
| 130 | + } |
| 131 | + instanceDeployKeys = append(instanceDeployKeys, paginatedInstancedeployKeys...) |
| 132 | + |
| 133 | + options.Page = resp.NextPage |
| 134 | + } |
| 135 | + |
| 136 | + // NOTE: this data source doesn't have a "real" id, but the query to the API |
| 137 | + // should actually return the same response for the same options, |
| 138 | + // therefore the `Public` field option is used as id. |
| 139 | + d.SetId(fmt.Sprintf("%b", options.Public)) |
| 140 | + if err := d.Set("deploy_keys", flattenGitlabInstanceDeployKeys(instanceDeployKeys)); err != nil { |
| 141 | + return diag.Errorf("error setting deploy_keys: %s", err) |
| 142 | + } |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +func flattenGitlabInstanceDeployKeys(keys []*gitlab.InstanceDeployKey) []interface{} { |
| 147 | + result := []interface{}{} |
| 148 | + for _, instanceDeployKey := range keys { |
| 149 | + values := map[string]interface{}{ |
| 150 | + "id": instanceDeployKey.ID, |
| 151 | + "title": instanceDeployKey.Title, |
| 152 | + "created_at": instanceDeployKey.CreatedAt.Format(time.RFC3339), |
| 153 | + "key": instanceDeployKey.Key, |
| 154 | + "fingerprint": instanceDeployKey.Fingerprint, |
| 155 | + "projects_with_write_access": flattenGitlabInstanceDeployKeysProjectsWithWriteAccess(instanceDeployKey.ProjectsWithWriteAccess), |
| 156 | + } |
| 157 | + result = append(result, values) |
| 158 | + } |
| 159 | + return result |
| 160 | +} |
| 161 | + |
| 162 | +func flattenGitlabInstanceDeployKeysProjectsWithWriteAccess(projects []*gitlab.DeployKeyProject) []interface{} { |
| 163 | + result := []interface{}{} |
| 164 | + for _, project := range projects { |
| 165 | + values := map[string]interface{}{ |
| 166 | + "id": project.ID, |
| 167 | + "description": project.Description, |
| 168 | + "name": project.Name, |
| 169 | + "name_with_namespace": project.NameWithNamespace, |
| 170 | + "path": project.Path, |
| 171 | + "path_with_namespace": project.PathWithNamespace, |
| 172 | + "created_at": project.CreatedAt.Format(time.RFC3339), |
| 173 | + } |
| 174 | + result = append(result, values) |
| 175 | + } |
| 176 | + return result |
| 177 | +} |
0 commit comments