Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Service Endpoint for Custom Services #628

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/tfhelper"
)

// ResourceServiceEndpointCustom schema and implementation for Custom service endpoint resource
func ResourceServiceEndpointCustom() *schema.Resource {
r := genBaseServiceEndpointResource(flattenServiceEndpointCustom, expandServiceEndpointCustom)
r.Schema["service_type"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The Service Type of the Custom service connection.",
}
r.Schema["server_url"] = &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
Required: true,
Description: "The server URL of the Custom service connection.",
}
r.Schema["username"] = &schema.Schema{
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("AZDO_Custom_SERVICE_CONNECTION_USERNAME", nil),
Description: "The username to use for the Custom service connection.",
Optional: true,
}
r.Schema["password"] = &schema.Schema{
Type: schema.TypeString,
DefaultFunc: schema.EnvDefaultFunc("AZDO_Custom_SERVICE_CONNECTION_PASSWORD", nil),
Description: "The password or token key to use for the Custom service connection.",
Sensitive: true,
Optional: true,
DiffSuppressFunc: tfhelper.DiffFuncSuppressSecretChanged,
}
secretHashKey, secretHashSchema := tfhelper.GenerateSecreteMemoSchema("password")
r.Schema[secretHashKey] = secretHashSchema
return r
}

func expandServiceEndpointCustom(d *schema.ResourceData) (*serviceendpoint.ServiceEndpoint, *uuid.UUID, error) {
serviceEndpoint, projectID := doBaseExpansion(d)
serviceEndpoint.Type = converter.String(d.Get("service_type").(string))
serviceEndpoint.Url = converter.String(d.Get("server_url").(string))
serviceEndpoint.Authorization = &serviceendpoint.EndpointAuthorization{
Parameters: &map[string]string{
"username": d.Get("username").(string),
"password": d.Get("password").(string),
},
Scheme: converter.String("UsernamePassword"),
}
return serviceEndpoint, projectID, nil
}

func flattenServiceEndpointCustom(d *schema.ResourceData, serviceEndpoint *serviceendpoint.ServiceEndpoint, projectID *uuid.UUID) {
doBaseFlattening(d, serviceEndpoint, projectID)
d.Set("service_type", *serviceEndpoint.Type)
d.Set("server_url", *serviceEndpoint.Url)
d.Set("username", (*serviceEndpoint.Authorization.Parameters)["username"])
tfhelper.HelpFlattenSecret(d, "password")
}
1 change: 1 addition & 0 deletions azuredevops/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Provider() *schema.Provider {
"azuredevops_serviceendpoint_azurerm": serviceendpoint.ResourceServiceEndpointAzureRM(),
"azuredevops_serviceendpoint_bitbucket": serviceendpoint.ResourceServiceEndpointBitBucket(),
"azuredevops_serviceendpoint_azuredevops": serviceendpoint.ResourceServiceEndpointAzureDevOps(),
"azuredevops_serviceendpoint_custom": serviceendpoint.ResourceServiceEndpointCustom(),
"azuredevops_serviceendpoint_dockerregistry": serviceendpoint.ResourceServiceEndpointDockerRegistry(),
"azuredevops_serviceendpoint_azurecr": serviceendpoint.ResourceServiceEndpointAzureCR(),
"azuredevops_serviceendpoint_github": serviceendpoint.ResourceServiceEndpointGitHub(),
Expand Down
70 changes: 70 additions & 0 deletions website/docs/r/serviceendpoint_custom.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
layout: "azuredevops"
page_title: "AzureDevops: azuredevops_serviceendpoint_custom"
description: |-
Manages a custom service endpoint within Azure DevOps, which can be used to authenticate to any external server using
basic authentication via a username and password.
---

# azuredevops_serviceendpoint_custom

Manages a custom service endpoint within Azure DevOps, which can be used to authenticate to any external server using
basic authentication via a username and password.

## Example Usage

```hcl
resource "azuredevops_project" "example" {
name = "Example Project"
visibility = "private"
version_control = "Git"
work_item_template = "Agile"
description = "Managed by Terraform"
}

resource "azuredevops_serviceendpoint_custom" "example" {
project_id = azuredevops_project.example.id
service_type = "custom type name"
server_url = "https://some-server.example.com"
username = "username"
password = "password"
service_endpoint_name = "Example Generic"
description = "Managed by Terraform"
}
```

## Argument Reference

The following arguments are supported:

- `project_id` - (Required) The ID of the project.
- `service_endpoint_name` - (Required) The service endpoint name.
- `service_type` - (Required) The Service Type of the server associated with the service endpoint.
- `server_url` - (Required) The URL of the server associated with the service endpoint.
- `username` - (Optional) The username used to authenticate to the server url using basic authentication.
- `password` - (Optional) The password or token key used to authenticate to the server url using basic authentication.
- `description` - (Optional) The Service Endpoint description. Defaults to `Managed by Terraform`.

Obs.: Access [Azure DevOps Service REST API 7.1 - Service Endpoints Types List](https://dev.azure.com/{organization}/_apis/serviceendpoint/types?api-version=7.1-preview.1) to get the Type.

## Attributes Reference

The following attributes are exported:

- `id` - The ID of the service endpoint.
- `project_id` - The ID of the project.
- `service_endpoint_name` - The name of the service endpoint.

## Relevant Links

- [Azure DevOps Service REST API 6.0 - Service Endpoints](https://docs.microsoft.com/en-us/rest/api/azure/devops/serviceendpoint/endpoints?view=azure-devops-rest-6.0)
- [Azure DevOps Service REST API 7.1 - Service Endpoints Types List](https://docs.microsoft.com/en-us/rest/api/azure/devops/serviceendpoint/types/list?view=azure-devops-rest-7.1&tabs=HTTP)

## Import

Azure DevOps Service Endpoint Generic can be imported using **projectID/serviceEndpointID** or
**projectName/serviceEndpointID**

```sh
terraform import azuredevops_serviceendpoint_custom.example 00000000-0000-0000-0000-000000000000/00000000-0000-0000-0000-000000000000
```