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

Created service end point for NuGet #8

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
444b6c0
Created service end point for NuGet
innagarc Mar 20, 2023
49a4a40
Create documentation
innagarc Mar 20, 2023
631bd49
Minor fixes
innagarc Mar 20, 2023
b9e4803
Modelled along the lines of jfrog_artifactory_v2
innagarc Mar 23, 2023
c35b828
Fix doc as per JFrog V2
innagarc Mar 23, 2023
73cef00
Added the third variant
innagarc Mar 23, 2023
390216c
Fix implementation
innagarc Mar 24, 2023
db16caf
Fix nugetkey
innagarc Mar 25, 2023
6f4e85d
Merge branch 'ni-main' into users/innagarc/add-nuget-service-conn
innagarc Mar 27, 2023
2ab7aa2
Cleanup
innagarc Mar 27, 2023
cc2f55d
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
2bb1f91
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
06b8799
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
c921b81
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
e7f1f3f
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
a411c09
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
98144e4
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
6abeb4e
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
67c3e20
Update azuredevops/internal/service/serviceendpoint/resource_servicee…
innagarc Mar 29, 2023
38b0a28
Put fixes after addressing review comments
innagarc Mar 29, 2023
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,60 @@
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"
)

// ResourceServiceEndpointNuget schema and implementation for NuGet service endpoint resource
func ResourceServiceEndpointNuget() *schema.Resource {
r := genBaseServiceEndpointResource(flattenServiceEndpointNuget, expandServiceEndpointNuget)

r.Schema["url"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.IsURLWithHTTPorHTTPS,
Description: "Url for the NuGet feed",
}

r.Schema["access_token"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Sensitive: true,
DiffSuppressFunc: tfhelper.DiffFuncSuppressSecretChanged,
ValidateFunc: validation.StringIsNotWhiteSpace,
Description: "The access token / ApiKey for NuGet feed",
}
// Add a spot in the schema to store the token secretly
stSecretHashKey, stSecretHashSchema := tfhelper.GenerateSecreteMemoSchema("access_token")
r.Schema[stSecretHashKey] = stSecretHashSchema

return r
}

// Convert internal Terraform data structure to an AzDO data structure
func expandServiceEndpointNuget(d *schema.ResourceData) (*serviceendpoint.ServiceEndpoint, *uuid.UUID, error) {
serviceEndpoint, projectID := doBaseExpansion(d)
serviceEndpoint.Authorization = &serviceendpoint.EndpointAuthorization{
Parameters: &map[string]string{
"apitoken": d.Get("access_token").(string),
},
Scheme: converter.String("Token"),
}
serviceEndpoint.Type = converter.String("externalnugetfeed")
serviceEndpoint.Url = converter.String(d.Get("url").(string))
return serviceEndpoint, projectID, nil
}

// Convert AzDO data structure to internal Terraform data structure
func flattenServiceEndpointNuget(d *schema.ResourceData, serviceEndpoint *serviceendpoint.ServiceEndpoint, projectID *uuid.UUID) {
doBaseFlattening(d, serviceEndpoint, projectID)

tfhelper.HelpFlattenSecret(d, "access_token")

d.Set("url", *serviceEndpoint.Url)
d.Set("access_token", (*serviceEndpoint.Authorization.Parameters)["apitoken"])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//go:build (all || resource_serviceendpoint_nuget) && !exclude_serviceendpoints
// +build all resource_serviceendpoint_nuget
// +build !exclude_serviceendpoints

package serviceendpoint

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/microsoft/azure-devops-go-api/azuredevops/v6/serviceendpoint"
"github.com/microsoft/terraform-provider-azuredevops/azdosdkmocks"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/client"
"github.com/microsoft/terraform-provider-azuredevops/azuredevops/internal/utils/converter"
"github.com/stretchr/testify/require"
)

var nugetTestServiceEndpointID = uuid.New()
var nugetRandomServiceEndpointProjectID = uuid.New()
var nugetTestServiceEndpointProjectID = &nugetRandomServiceEndpointProjectID

var nugetTestServiceEndpoint = serviceendpoint.ServiceEndpoint{
Authorization: &serviceendpoint.EndpointAuthorization{
Parameters: &map[string]string{
"apitoken": "NUGET_TEST_access_token",
},
Scheme: converter.String("Token"),
},
Id: &nugetTestServiceEndpointID,
Name: converter.String("UNIT_TEST_CONN_NAME"),
Owner: converter.String("library"),
Type: converter.String("externalnugetfeed"),
Url: converter.String("https://api.nuget.org/v3/index.json"),
ServiceEndpointProjectReferences: &[]serviceendpoint.ServiceEndpointProjectReference{
{
ProjectReference: &serviceendpoint.ProjectReference{
Id: nugetTestServiceEndpointProjectID,
},
Name: converter.String("UNIT_TEST_CONN_NAME"),
Description: converter.String("UNIT_TEST_CONN_DESCRIPTION"),
},
},
}

// verifies that the flatten/expand round trip yields the same service endpoint
func TestServiceEndpointNuget_ExpandFlatten_Roundtrip(t *testing.T) {
resourceData := schema.TestResourceDataRaw(t, ResourceServiceEndpointNuget().Schema, nil)
flattenServiceEndpointNuget(resourceData, &nugetTestServiceEndpoint, nugetTestServiceEndpointProjectID)

serviceEndpointAfterRoundTrip, projectID, err := expandServiceEndpointNuget(resourceData)

require.Equal(t, nugetTestServiceEndpoint, *serviceEndpointAfterRoundTrip)
require.Equal(t, nugetTestServiceEndpointProjectID, projectID)
require.Nil(t, err)
}

// verifies that if an error is produced on create, the error is not swallowed
func TestServiceEndpointNuget_Create_DoesNotSwallowError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := ResourceServiceEndpointNuget()
resourceData := schema.TestResourceDataRaw(t, r.Schema, nil)
flattenServiceEndpointNuget(resourceData, &nugetTestServiceEndpoint, nugetTestServiceEndpointProjectID)

buildClient := azdosdkmocks.NewMockServiceendpointClient(ctrl)
clients := &client.AggregatedClient{ServiceEndpointClient: buildClient, Ctx: context.Background()}

expectedArgs := serviceendpoint.CreateServiceEndpointArgs{Endpoint: &nugetTestServiceEndpoint}
buildClient.
EXPECT().
CreateServiceEndpoint(clients.Ctx, expectedArgs).
Return(nil, errors.New("CreateServiceEndpoint() Failed")).
Times(1)

err := r.Create(resourceData, clients)
require.Contains(t, err.Error(), "CreateServiceEndpoint() Failed")
}

// verifies that if an error is produced on a read, it is not swallowed
func TestServiceEndpointNuget_Read_DoesNotSwallowError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := ResourceServiceEndpointNuget()
resourceData := schema.TestResourceDataRaw(t, r.Schema, nil)
flattenServiceEndpointNuget(resourceData, &nugetTestServiceEndpoint, nugetTestServiceEndpointProjectID)

buildClient := azdosdkmocks.NewMockServiceendpointClient(ctrl)
clients := &client.AggregatedClient{ServiceEndpointClient: buildClient, Ctx: context.Background()}

expectedArgs := serviceendpoint.GetServiceEndpointDetailsArgs{
EndpointId: nugetTestServiceEndpoint.Id,
Project: converter.String(nugetTestServiceEndpointProjectID.String()),
}
buildClient.
EXPECT().
GetServiceEndpointDetails(clients.Ctx, expectedArgs).
Return(nil, errors.New("GetServiceEndpoint() Failed")).
Times(1)

err := r.Read(resourceData, clients)
require.Contains(t, err.Error(), "GetServiceEndpoint() Failed")
}

// verifies that if an error is produced on a delete, it is not swallowed
func TestServiceEndpointNuget_Delete_DoesNotSwallowError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := ResourceServiceEndpointNuget()
resourceData := schema.TestResourceDataRaw(t, r.Schema, nil)
flattenServiceEndpointNuget(resourceData, &nugetTestServiceEndpoint, nugetTestServiceEndpointProjectID)

buildClient := azdosdkmocks.NewMockServiceendpointClient(ctrl)
clients := &client.AggregatedClient{ServiceEndpointClient: buildClient, Ctx: context.Background()}

expectedArgs := serviceendpoint.DeleteServiceEndpointArgs{
EndpointId: nugetTestServiceEndpoint.Id,
ProjectIds: &[]string{
nugetTestServiceEndpointProjectID.String(),
},
}
buildClient.
EXPECT().
DeleteServiceEndpoint(clients.Ctx, expectedArgs).
Return(errors.New("DeleteServiceEndpoint() Failed")).
Times(1)

err := r.Delete(resourceData, clients)
require.Contains(t, err.Error(), "DeleteServiceEndpoint() Failed")
}

// verifies that if an error is produced on an update, it is not swallowed
func TestServiceEndpointNuget_Update_DoesNotSwallowError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

r := ResourceServiceEndpointNuget()
resourceData := schema.TestResourceDataRaw(t, r.Schema, nil)
flattenServiceEndpointNuget(resourceData, &nugetTestServiceEndpoint, nugetTestServiceEndpointProjectID)

buildClient := azdosdkmocks.NewMockServiceendpointClient(ctrl)
clients := &client.AggregatedClient{ServiceEndpointClient: buildClient, Ctx: context.Background()}

expectedArgs := serviceendpoint.UpdateServiceEndpointArgs{
Endpoint: &nugetTestServiceEndpoint,
EndpointId: nugetTestServiceEndpoint.Id,
}

buildClient.
EXPECT().
UpdateServiceEndpoint(clients.Ctx, expectedArgs).
Return(nil, errors.New("UpdateServiceEndpoint() Failed")).
Times(1)

err := r.Update(resourceData, clients)
require.Contains(t, err.Error(), "UpdateServiceEndpoint() Failed")
}
1 change: 1 addition & 0 deletions azuredevops/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func Provider() *schema.Provider {
"azuredevops_serviceendpoint_sonarcloud": serviceendpoint.ResourceServiceEndpointSonarCloud(),
"azuredevops_serviceendpoint_ssh": serviceendpoint.ResourceServiceEndpointSSH(),
"azuredevops_serviceendpoint_npm": serviceendpoint.ResourceServiceEndpointNpm(),
"azuredevops_serviceendpoint_nuget": serviceendpoint.ResourceServiceEndpointNuget(),
"azuredevops_serviceendpoint_generic": serviceendpoint.ResourceServiceEndpointGeneric(),
"azuredevops_serviceendpoint_generic_git": serviceendpoint.ResourceServiceEndpointGenericGit(),
"azuredevops_serviceendpoint_externaltfs": serviceendpoint.ResourceServiceEndpointExternalTFS(),
Expand Down
1 change: 1 addition & 0 deletions azuredevops/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestProvider_HasChildResources(t *testing.T) {
"azuredevops_serviceendpoint_sonarcloud",
"azuredevops_serviceendpoint_ssh",
"azuredevops_serviceendpoint_npm",
"azuredevops_serviceendpoint_nuget",
"azuredevops_serviceendpoint_generic",
"azuredevops_serviceendpoint_generic_git",
"azuredevops_serviceendpoint_octopusdeploy",
Expand Down
3 changes: 3 additions & 0 deletions website/azuredevops.erb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@
<li>
<a href="/docs/providers/azuredevops/r/serviceendpoint_npm.html">azuredevops_serviceendpoint_npm</a>
</li>
<li>
<a href="/docs/providers/azuredevops/r/serviceendpoint_npm.html">azuredevops_serviceendpoint_nuget</a>
</li>
<li>
<a href="/docs/providers/azuredevops/r/serviceendpoint_octopusdeploy.html">azuredevops_serviceendpoint_octopusdeploy</a>
</li>
Expand Down
63 changes: 63 additions & 0 deletions website/docs/r/serviceendpoint_nuget.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
layout: "azuredevops"
page_title: "AzureDevops: azuredevops_serviceendpoint_nuget"
description: |-
Manages a NuGet server endpoint within Azure DevOps organization.
---

# azuredevops_serviceendpoint_nuget

Manages a NuGet service endpoint within Azure DevOps.

## 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_nuget" "example" {
project_id = azuredevops_project.example.id
service_endpoint_name = "Example NuGet"
url = "https://api.nuget.org/v3/index.json"
access_token = "AbcDEf123_0x"
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.
- `url` - (Required) URL of the NuGet feed to connect with.
- `access_token` - (Required) The access-token/ApiKey for NuGet feed.
- `description` - (Optional) The Service Endpoint description.

## 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 Service Endpoint name.

## Relevant Links

- [Azure DevOps Service REST API 6.0 - Endpoints](https://docs.microsoft.com/en-us/rest/api/azure/devops/serviceendpoint/endpoints?view=azure-devops-rest-6.0)
- [Azure DevOps Service Connections](https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml)
- [NuGet ApiKey](https://learn.microsoft.com/en-in/nuget/nuget-org/scoped-api-keys)
- [NuGet packages in Azure Artifacts](https://learn.microsoft.com/en-us/azure/devops/artifacts/get-started-nuget?view=azure-devops&tabs=windows)

## Import

Azure DevOps Service Endpoint NuGet can be imported using the **projectID/serviceEndpointID**, e.g.

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