|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/go-github/v66/github" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceGithubOrganizationAppInstallations() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Read: dataSourceGithubOrganizationAppInstallationsRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "installations": { |
| 16 | + Type: schema.TypeList, |
| 17 | + Computed: true, |
| 18 | + Elem: &schema.Resource{ |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "slug": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Computed: true, |
| 23 | + }, |
| 24 | + "node_id": { |
| 25 | + Type: schema.TypeInt, |
| 26 | + Computed: true, |
| 27 | + }, |
| 28 | + "app_id": { |
| 29 | + Type: schema.TypeInt, |
| 30 | + Computed: true, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func dataSourceGithubOrganizationAppInstallationsRead(d *schema.ResourceData, meta interface{}) error { |
| 40 | + owner := meta.(*Owner).name |
| 41 | + |
| 42 | + client := meta.(*Owner).v3client |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + options := &github.ListOptions{ |
| 46 | + PerPage: 100, |
| 47 | + } |
| 48 | + |
| 49 | + results := make([]map[string]interface{}, 0) |
| 50 | + for { |
| 51 | + appInstallations, resp, err := client.Organizations.ListInstallations(ctx, owner, options) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + results = append(results, flattenGitHubAppInstallations(appInstallations.Installations)...) |
| 57 | + if resp.NextPage == 0 { |
| 58 | + break |
| 59 | + } |
| 60 | + |
| 61 | + options.Page = resp.NextPage |
| 62 | + } |
| 63 | + |
| 64 | + d.SetId(owner) |
| 65 | + err := d.Set("installations", results) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func flattenGitHubAppInstallations(orgAppInstallations []*github.Installation) []map[string]interface{} { |
| 74 | + results := make([]map[string]interface{}, 0) |
| 75 | + |
| 76 | + if orgAppInstallations == nil { |
| 77 | + return results |
| 78 | + } |
| 79 | + |
| 80 | + for _, appInstallation := range orgAppInstallations { |
| 81 | + result := make(map[string]interface{}) |
| 82 | + |
| 83 | + result["slug"] = appInstallation.AppSlug |
| 84 | + result["node_id"] = appInstallation.NodeID |
| 85 | + result["app_id"] = appInstallation.AppID |
| 86 | + |
| 87 | + results = append(results, result) |
| 88 | + } |
| 89 | + |
| 90 | + return results |
| 91 | +} |
0 commit comments