forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_repository_environment_deployment_policies.go
More file actions
75 lines (65 loc) · 1.85 KB
/
data_source_github_repository_environment_deployment_policies.go
File metadata and controls
75 lines (65 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package github
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubRepositoryEnvironmentDeploymentPolicies() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the GitHub repository.",
},
"environment": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the environment.",
},
"policies": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
"pattern": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceGithubRepositoryEnvironmentDeploymentPoliciesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*Owner).v3client
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
environmentName := d.Get("environment").(string)
policies, _, err := client.Repositories.ListDeploymentBranchPolicies(ctx, owner, repoName, environmentName)
if err != nil {
return diag.FromErr(err)
}
results := make([]map[string]any, 0)
for _, policy := range policies.BranchPolicies {
policyMap := make(map[string]any)
policyMap["type"] = policy.GetType()
policyMap["pattern"] = policy.GetName()
results = append(results, policyMap)
}
d.SetId(fmt.Sprintf("%s:%s", repoName, environmentName))
err = d.Set("policies", results)
if err != nil {
return diag.FromErr(err)
}
return nil
}