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
|
package serviceendpoint
import (
"strings"
"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/serviceendpoint"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/converter"
)
const (
personalAccessTokenExternalTFS = "personal_access_token"
)
func ResourceServiceEndpointExternalTFS() *schema.Resource {
r := genBaseServiceEndpointResource(flattenServiceEndpointExternalTFS, expandServiceEndpointExternalTFS)
r.Schema["connection_url"] = &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
Required: true,
Description: "URL of the Azure DevOps organization or the TFS Project Collection to connect to.",
}
authPersonal := &schema.Resource{
Schema: map[string]*schema.Schema{
personalAccessTokenExternalTFS: {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("AZDO_PERSONAL_ACCESS_TOKEN", nil),
Description: "Personal access tokens are applicable only for connections targeting Azure DevOps organization or TFS 2017 (and higher)",
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
r.Schema["auth_personal"] = &schema.Schema{
Type: schema.TypeSet,
MinItems: 1,
MaxItems: 1,
Elem: authPersonal,
Required: true,
}
return r
}
func expandServiceEndpointExternalTFS(d *schema.ResourceData) (*serviceendpoint.ServiceEndpoint, *uuid.UUID, error) {
serviceEndpoint, projectID := doBaseExpansion(d)
serviceEndpoint.Type = converter.String("externaltfs")
serviceEndpoint.Url = converter.String(d.Get("connection_url").(string))
scheme := "Token"
parameters := map[string]string{}
if config, ok := d.GetOk("auth_personal"); ok {
scheme = "Token"
parameters = expandAuthPersonalSetExternalTFS(config.(*schema.Set))
}
serviceEndpoint.Authorization = &serviceendpoint.EndpointAuthorization{
Parameters: ¶meters,
Scheme: &scheme,
}
return serviceEndpoint, projectID, nil
}
func expandAuthPersonalSetExternalTFS(d *schema.Set) map[string]string {
authPerson := make(map[string]string)
val := d.List()[0].(map[string]interface{})
authPerson["apitoken"] = val[personalAccessTokenExternalTFS].(string)
return authPerson
}
func flattenServiceEndpointExternalTFS(
d *schema.ResourceData,
serviceEndpoint *serviceendpoint.ServiceEndpoint,
projectID *uuid.UUID,
) {
doBaseFlattening(d, serviceEndpoint, projectID)
if strings.EqualFold(*serviceEndpoint.Authorization.Scheme, "Token") {
d.Set("auth_personal", &[]map[string]interface{}{
{
personalAccessTokenExternalTFS: (*serviceEndpoint.Authorization.Parameters)["apitoken"],
},
})
}
d.Set("connection_url", *serviceEndpoint.Url)
}
|