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 10 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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package serviceendpoint

import (
"fmt"
"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"
"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: func(i interface{}, key string) (_ []string, errors []error) {
url, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", key))
return
}
if strings.HasSuffix(url, "/") {
errors = append(errors, fmt.Errorf("%q should not end with slash, got %q.", key, url))
return
}
return validation.IsURLWithHTTPorHTTPS(url, key)
},
Description: "Url for the Nuget Feed",
}

patHashKey, patHashSchema := tfhelper.GenerateSecreteMemoSchema("token")
at := &schema.Resource{
Schema: map[string]*schema.Schema{
"token": {
Description: "The Nuget Feed access token.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
DiffSuppressFunc: tfhelper.DiffFuncSuppressSecretChanged,
},
patHashKey: patHashSchema,
},
}

keyHashKey, keyHashSchema := tfhelper.GenerateSecreteMemoSchema("key")
ak := &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Description: "The Nuget Feed API key.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
DiffSuppressFunc: tfhelper.DiffFuncSuppressSecretChanged,
},
keyHashKey: keyHashSchema,
},
}

patHashKeyU, patHashSchemaU := tfhelper.GenerateSecreteMemoSchema("username")
patHashKeyP, patHashSchemaP := tfhelper.GenerateSecreteMemoSchema("password")
aup := &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Description: "The Nuget feed user name.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
DiffSuppressFunc: tfhelper.DiffFuncSuppressSecretChanged,
},
patHashKeyU: patHashSchemaU,
"password": {
Description: "The Nuget feed password.",
Type: schema.TypeString,
Required: true,
Sensitive: true,
DiffSuppressFunc: tfhelper.DiffFuncSuppressSecretChanged,
},
patHashKeyP: patHashSchemaP,
},
}

r.Schema["authentication_token"] = &schema.Schema{
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: at,
ExactlyOneOf: []string{"authentication_basic", "authentication_token", "authentication_none"},
}

r.Schema["authentication_none"] = &schema.Schema{
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: ak,
}

r.Schema["authentication_basic"] = &schema.Schema{
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: aup,
}

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.Type = converter.String("externalnugetfeed")
serviceEndpoint.Url = converter.String(d.Get("url").(string))
authScheme := "Token"

authParams := make(map[string]string)

if x, ok := d.GetOk("authentication_token"); ok {
authScheme = "Token"
msi := x.([]interface{})[0].(map[string]interface{})
authParams["apitoken"] = expandSecret(msi, "token")
} else if x, ok := d.GetOk("authentication_none"); ok {
authScheme = "None"
msi := x.([]interface{})[0].(map[string]interface{})
authParams["nugetkey"] = expandSecret(msi, "key")
} else if x, ok := d.GetOk("authentication_basic"); ok {
authScheme = "UsernamePassword"
msi := x.([]interface{})[0].(map[string]interface{})
authParams["username"] = expandSecret(msi, "username")
authParams["password"] = expandSecret(msi, "password")
}
serviceEndpoint.Authorization = &serviceendpoint.EndpointAuthorization{
Parameters: &authParams,
Scheme: &authScheme,
}

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)
if strings.EqualFold(*serviceEndpoint.Authorization.Scheme, "Token") {
auth := make(map[string]interface{})
if x, ok := d.GetOk("authentication_token"); ok {
authList := x.([]interface{})[0].(map[string]interface{})
if len(authList) > 0 {
newHash, hashKey := tfhelper.HelpFlattenSecretNested(d, "authentication_token", authList, "token")
auth[hashKey] = newHash
}
} else if serviceEndpoint.Authorization != nil && serviceEndpoint.Authorization.Parameters != nil {
auth["token"] = (*serviceEndpoint.Authorization.Parameters)["apitoken"]
}
d.Set("authentication_token", []interface{}{auth})
} else if strings.EqualFold(*serviceEndpoint.Authorization.Scheme, "None") {
auth := make(map[string]interface{})
if x, ok := d.GetOk("authentication_none"); ok {
authList := x.([]interface{})[0].(map[string]interface{})
if len(authList) > 0 {
newHash, hashKey := tfhelper.HelpFlattenSecretNested(d, "authentication_none", authList, "key")
auth[hashKey] = newHash
}
} else if serviceEndpoint.Authorization != nil && serviceEndpoint.Authorization.Parameters != nil {
auth["key"] = (*serviceEndpoint.Authorization.Parameters)["nugetkey"]
}
d.Set("authentication_none", []interface{}{auth})
} else if strings.EqualFold(*serviceEndpoint.Authorization.Scheme, "UsernamePassword") {
auth := make(map[string]interface{})
if old, ok := d.GetOk("authentication_basic"); ok {
oldAuthList := old.([]interface{})[0].(map[string]interface{})
if len(oldAuthList) > 0 {
newHash, hashKey := tfhelper.HelpFlattenSecretNested(d, "authentication_basic", oldAuthList, "password")
auth[hashKey] = newHash
newHash, hashKey = tfhelper.HelpFlattenSecretNested(d, "authentication_basic", oldAuthList, "username")
auth[hashKey] = newHash
}
} else if serviceEndpoint.Authorization != nil && serviceEndpoint.Authorization.Parameters != nil {
auth["password"] = (*serviceEndpoint.Authorization.Parameters)["password"]
auth["username"] = (*serviceEndpoint.Authorization.Parameters)["username"]
}
d.Set("authentication_basic", []interface{}{auth})
} else {
panic(fmt.Errorf("inconsistent authorization scheme. Expected: (Token, None, UsernamePassword) , but got %s", *serviceEndpoint.Authorization.Scheme))
}

d.Set("url", *serviceEndpoint.Url)
}
Loading