summaryrefslogtreecommitdiff
path: root/azuredevops/internal/service/graph/data_groups.go
blob: b7f67333c166cb091894769a5923ca93a541b542 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package graph

import (
	"fmt"

	"github.com/google/uuid"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
	"github.com/microsoft/azure-devops-go-api/azuredevops/v6/graph"
	"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/client"
	"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils"
	"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/tfhelper"
)

// DataGroups schema and implementation for group data source
func DataGroups() *schema.Resource {
	return &schema.Resource{
		Read: dataSourceGroupsRead,
		Schema: map[string]*schema.Schema{
			"project_id": {
				Type:         schema.TypeString,
				Optional:     true,
				ValidateFunc: validation.StringIsNotWhiteSpace,
			},
			"groups": {
				Type:     schema.TypeSet,
				Computed: true,
				Set:      getGroupHash,
				Elem: &schema.Resource{
					Schema: map[string]*schema.Schema{
						"descriptor": {
							Type:     schema.TypeString,
							Computed: true,
						},

						"origin": {
							Type:     schema.TypeString,
							Computed: true,
						},

						"origin_id": {
							Type:     schema.TypeString,
							Optional: true,
						},

						"mail_address": {
							Type:     schema.TypeString,
							Optional: true,
						},

						"display_name": {
							Type:     schema.TypeString,
							Optional: true,
						},

						"description": {
							Type:     schema.TypeString,
							Optional: true,
						},

						"url": {
							Type:     schema.TypeString,
							Computed: true,
						},

						"domain": {
							Type:     schema.TypeString,
							Computed: true,
						},

						"principal_name": {
							Type:     schema.TypeString,
							Computed: true,
						},
					},
				},
			},
		},
	}
}

// Performs a lookup of a project group. This involves the following actions:
//
//	(1) Identify AzDO graph descriptor for the project in which the group exists
//	(2) Query for all AzDO groups that exist within the project. This leverages the AzDO graph descriptor for the project.
//		This involves querying a paginated API, so multiple API calls may be needed for this step.
//	(3) Select group that has the name identified by the schema
func dataSourceGroupsRead(d *schema.ResourceData, m interface{}) error {
	clients := m.(*client.AggregatedClient)
	projectID := d.Get("project_id").(string)

	projectDescriptor, err := getProjectDescriptor(clients, projectID)
	if err != nil {
		if utils.ResponseWasNotFound(err) {
			return fmt.Errorf("Project with with ID %s was not found. Error: %v", projectID, err)
		}
		return fmt.Errorf("Error finding descriptor for project with ID %s. Error: %v", projectID, err)
	}

	groups, err := getGroupsForDescriptor(clients, projectDescriptor)
	if err != nil {
		errMsg := "Error finding groups"
		if projectID != "" {
			errMsg = fmt.Sprintf("%s for project with ID %s", errMsg, projectID)
		}
		return fmt.Errorf("%s. Error: %v", errMsg, err)
	}

	fgroups, err := flattenGroups(groups)
	if err != nil {
		return fmt.Errorf("Error flatten groups. Error: %w", err)
	}

	d.SetId("groups-" + uuid.New().String())
	d.Set("groups", fgroups)
	return nil
}

func getGroupHash(v interface{}) int {
	return tfhelper.HashString(v.(map[string]interface{})["descriptor"].(string))
}

func flattenGroups(groups *[]graph.GraphGroup) ([]interface{}, error) {
	if groups == nil {
		return []interface{}{}, nil
	}

	results := make([]interface{}, len(*groups))
	for i, group := range *groups {
		s := make(map[string]interface{})

		if group.Descriptor != nil {
			s["descriptor"] = *group.Descriptor
		} else {
			return nil, fmt.Errorf("Group Object does not contain a descriptor")
		}
		if group.DisplayName != nil {
			s["display_name"] = *group.DisplayName
		}
		if group.Url != nil {
			s["url"] = *group.Url
		}
		if group.Origin != nil {
			s["origin"] = *group.Origin
		}
		if group.OriginId != nil {
			s["origin_id"] = *group.OriginId
		}
		if group.Domain != nil {
			s["domain"] = *group.Domain
		}
		if group.MailAddress != nil {
			s["mail_address"] = *group.MailAddress
		}
		if group.PrincipalName != nil {
			s["principal_name"] = *group.PrincipalName
		}
		if group.Description != nil {
			s["description"] = *group.Description
		}

		results[i] = s
	}
	return results, nil
}