|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/mitchellh/hashstructure" |
| 10 | + gitlab "github.com/xanzy/go-gitlab" |
| 11 | +) |
| 12 | + |
| 13 | +var _ = registerDataSource("gitlab_repository_tree", func() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Description: `The ` + "`gitlab_repository_tree`" + ` data source allows details of directories and files in a repository to be retrieved. |
| 16 | +
|
| 17 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/repositories.html#list-repository-tree)`, |
| 18 | + |
| 19 | + ReadContext: dataSourceGitlabRepositoryTreeRead, |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "project": { |
| 22 | + Description: "The ID or full path of the project owned by the authenticated user.", |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + "ref": { |
| 27 | + Description: "The name of a repository branch or tag.", |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + }, |
| 31 | + "path": { |
| 32 | + Description: "The path inside repository. Used to get content of subdirectories.", |
| 33 | + Type: schema.TypeString, |
| 34 | + Optional: true, |
| 35 | + }, |
| 36 | + "recursive": { |
| 37 | + Description: "Boolean value used to get a recursive tree (false by default).", |
| 38 | + Type: schema.TypeBool, |
| 39 | + Optional: true, |
| 40 | + }, |
| 41 | + "tree": { |
| 42 | + Description: "The list of files/directories returned by the search", |
| 43 | + Type: schema.TypeList, |
| 44 | + Computed: true, |
| 45 | + Elem: &schema.Resource{ |
| 46 | + Schema: map[string]*schema.Schema{ |
| 47 | + "id": { |
| 48 | + Description: "The SHA-1 hash of the tree or blob in the repository.", |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "name": { |
| 53 | + Description: "Name of the blob or tree in the repository", |
| 54 | + Type: schema.TypeString, |
| 55 | + Computed: true, |
| 56 | + }, |
| 57 | + "type": { |
| 58 | + Description: "Type of object in the repository. Can be either type tree or of type blob", |
| 59 | + Type: schema.TypeString, |
| 60 | + Computed: true, |
| 61 | + }, |
| 62 | + "path": { |
| 63 | + Description: "Path of the object inside of the repository.", |
| 64 | + Type: schema.TypeString, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + "mode": { |
| 68 | + Description: "Unix access mode of the file in the repository.", |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | +}) |
| 78 | + |
| 79 | +func dataSourceGitlabRepositoryTreeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 80 | + client := meta.(*gitlab.Client) |
| 81 | + project := d.Get("project").(string) |
| 82 | + |
| 83 | + options := &gitlab.ListTreeOptions{ |
| 84 | + ListOptions: gitlab.ListOptions{ |
| 85 | + PerPage: 20, |
| 86 | + Page: 1, |
| 87 | + }, |
| 88 | + Path: gitlab.String(d.Get("path").(string)), |
| 89 | + Ref: gitlab.String(d.Get("ref").(string)), |
| 90 | + Recursive: gitlab.Bool(d.Get("recursive").(bool)), |
| 91 | + } |
| 92 | + |
| 93 | + var nodes []*gitlab.TreeNode |
| 94 | + for options.Page != 0 { |
| 95 | + |
| 96 | + paginatedNodes, resp, err := client.Repositories.ListTree(project, options, gitlab.WithContext(ctx)) |
| 97 | + if err != nil { |
| 98 | + return diag.FromErr(err) |
| 99 | + } |
| 100 | + |
| 101 | + nodes = append(nodes, paginatedNodes...) |
| 102 | + |
| 103 | + options.Page = resp.NextPage |
| 104 | + } |
| 105 | + |
| 106 | + optionsHash, err := hashstructure.Hash(&options, nil) |
| 107 | + if err != nil { |
| 108 | + return diag.FromErr(err) |
| 109 | + } |
| 110 | + |
| 111 | + d.SetId(fmt.Sprintf("%s:%d", project, optionsHash)) |
| 112 | + if err := d.Set("tree", flattenGitlabRepositoryTree(project, nodes)); err != nil { |
| 113 | + return diag.Errorf("failed to set repository tree nodes to state: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func flattenGitlabRepositoryTree(project string, treeNodes []*gitlab.TreeNode) []interface{} { |
| 120 | + treeNodeList := []interface{}{} |
| 121 | + |
| 122 | + for _, node := range treeNodes { |
| 123 | + |
| 124 | + values := map[string]interface{}{ |
| 125 | + "id": project, |
| 126 | + "name": node.Name, |
| 127 | + "type": node.Type, |
| 128 | + "path": node.Path, |
| 129 | + "mode": node.Mode, |
| 130 | + } |
| 131 | + |
| 132 | + treeNodeList = append(treeNodeList, values) |
| 133 | + } |
| 134 | + return treeNodeList |
| 135 | +} |
0 commit comments