summaryrefslogtreecommitdiff
path: root/azuredevops/internal/service/serviceendpoint/resource_serviceendpoint_externaltfs.go
blob: f50e1a7aa6f687473e486b228363dab2802d186f (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
package serviceendpoint

import (

	"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: &parameters,
		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)

  d.Get("")
	d.Set("connection_url", *serviceEndpoint.Url)
}