Skip to content

Commit

Permalink
Introduce jfrog-client-go for future migration
Browse files Browse the repository at this point in the history
Using jfrog/jfrog-client-go instead of atlassian/go-artifactory will
make it easier to integrate new features and keep things consistent.
However, migrating all current features over to jfrog-client-go is a
sizeable task. This commit introduces it alongside go-artifactory,
allowing a feature to use either. Now, any new features can use
jfrog-client-go, and old features can be migrated over time.
  • Loading branch information
Travis Foster committed Sep 8, 2020
1 parent 74e1dba commit ba711e0
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 79 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
[![Actions Status](https://github.com/atlassian/terraform-provider-artifactory/workflows/build/badge.svg)](https://github.com/atlassian/terraform-provider-artifactory/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/atlassian/terraform-provider-artifactory)](https://goreportcard.com/report/github.com/atlassian/terraform-provider-artifactory)

To use this provider in your Terraform module, follow the documentation [here](website/docs/index.html.markdown).

## Build the Provider
If you're building the provider, follow the instructions to [install it as a plugin](https://www.terraform.io/docs/plugins/basics.html#installing-a-plugin).
After placing it into your plugins directory, run `terraform init` to initialize it.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/atlassian/terraform-provider-artifactory
require (
github.com/atlassian/go-artifactory/v2 v2.5.0
github.com/hashicorp/terraform v0.12.29
github.com/jfrog/jfrog-client-go v0.13.1
github.com/stretchr/testify v1.5.1
)

Expand Down
48 changes: 48 additions & 0 deletions go.sum

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pkg/artifactory/datasource_artifactory_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"github.com/atlassian/go-artifactory/v2/artifactory"
"github.com/hashicorp/terraform/helper/schema"
"io"
"os"
Expand Down Expand Up @@ -82,7 +81,7 @@ func dataSourceArtifactoryFile() *schema.Resource {
}

func dataSourceFileRead(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

repository := d.Get("repository").(string)
path := d.Get("path").(string)
Expand Down
3 changes: 1 addition & 2 deletions pkg/artifactory/datasource_artifactory_fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package artifactory
import (
"context"
"fmt"
"github.com/atlassian/go-artifactory/v2/artifactory"
"github.com/atlassian/go-artifactory/v2/artifactory/v1"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -70,7 +69,7 @@ func dataSourceArtifactoryFileInfo() *schema.Resource {
}

func dataSourceFileInfoRead(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

repository := d.Get("repository").(string)
path := d.Get("path").(string)
Expand Down
73 changes: 55 additions & 18 deletions pkg/artifactory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ import (
"fmt"
"net/http"

"github.com/atlassian/go-artifactory/v2/artifactory"
artifactoryold "github.com/atlassian/go-artifactory/v2/artifactory"
"github.com/atlassian/go-artifactory/v2/artifactory/transport"
artifactorynew "github.com/jfrog/jfrog-client-go/artifactory"
"github.com/jfrog/jfrog-client-go/artifactory/usage"
"github.com/jfrog/jfrog-client-go/artifactory/auth"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/jfrog-client-go/config"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/version"
)

var ProviderVersion = "2.1.0"

type ArtClient struct {
ArtOld *artifactoryold.Artifactory
ArtNew *artifactorynew.ArtifactoryServicesManager
}

// Artifactory Provider that supports configuration via username+password or a token
// Supported resources are repos, users, groups, replications, and permissions
func Provider() terraform.ResourceProvider {
Expand All @@ -34,14 +47,6 @@ func Provider() terraform.ResourceProvider {
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_PASSWORD", nil),
ConflictsWith: []string{"access_token", "api_key"},
},
"token": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
DefaultFunc: schema.EnvDefaultFunc("ARTIFACTORY_TOKEN", nil),
ConflictsWith: []string{"api_key"},
Deprecated: "Since v1.5. Renamed to api_key",
},
"api_key": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -93,43 +98,75 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
apiKey := d.Get("api_key").(string)
accessToken := d.Get("access_token").(string)

// Deprecated
token := d.Get("token").(string)
log.SetLogger(log.NewLogger(log.INFO, nil))

var client *http.Client
details := auth.NewArtifactoryDetails()

url := d.Get("url").(string)
if url[len(url)-1] != '/' {
url += "/"
}
details.SetUrl(url)

if username != "" && password != "" {
details.SetUser(username)
details.SetPassword(password)
tp := transport.BasicAuth{
Username: username,
Password: password,
}
client = tp.Client()
} else if apiKey != "" {
details.SetApiKey(apiKey)
tp := &transport.ApiKeyAuth{
ApiKey: apiKey,
}
client = tp.Client()
} else if accessToken != "" {
details.SetAccessToken(accessToken)
tp := &transport.AccessTokenAuth{
AccessToken: accessToken,
}
client = tp.Client()
} else if token != "" {
tp := &transport.ApiKeyAuth{
ApiKey: token,
}
client = tp.Client()
} else {
return nil, fmt.Errorf("either [username, password] or [api_key] or [access_token] must be set to use provider")
}

rt, err := artifactory.NewClient(d.Get("url").(string), client)
config, err := config.NewConfigBuilder().
SetServiceDetails(details).
SetDryRun(false).
Build()

if err != nil {
return nil, err
} else if _, resp, err := rt.V1.System.Ping(context.Background()); err != nil {
}

rtold, err := artifactoryold.NewClient(d.Get("url").(string), client)

if err != nil {
return nil, err
}

rtnew, err := artifactorynew.New(&details, config)

if err != nil {
return nil, err
} else if _, resp, err := rtold.V1.System.Ping(context.Background()); err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to ping server. Got %d", resp.StatusCode)
} else if _, err := rtnew.Ping(); err != nil {
return nil, err
}

productid := "terraform-provider-artifactory/" + ProviderVersion
commandid := "Terraform/" + version.Version
usage.SendReportUsage(productid, commandid, rtnew)

rt := &ArtClient{
ArtOld: rtold,
ArtNew: rtnew,
}

return rt, nil
Expand Down
7 changes: 3 additions & 4 deletions pkg/artifactory/resource_artifactory_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"strconv"

"github.com/atlassian/go-artifactory/v2/artifactory"
v1 "github.com/atlassian/go-artifactory/v2/artifactory/v1"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -54,7 +53,7 @@ func packApiKey(apiKey *v1.ApiKey, d *schema.ResourceData) error {
}

func resourceApiKeyCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

apiKey, _, err := c.V1.Security.CreateApiKey(context.Background())
if err != nil {
Expand All @@ -66,7 +65,7 @@ func resourceApiKeyCreate(d *schema.ResourceData, m interface{}) error {
}

func resourceApiKeyRead(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

apiKey, resp, err := c.V1.Security.GetApiKey(context.Background())
if resp.StatusCode == http.StatusNotFound {
Expand All @@ -80,7 +79,7 @@ func resourceApiKeyRead(d *schema.ResourceData, m interface{}) error {
}

func resourceApiKeyDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld
_, resp, err := c.V1.Security.RevokeApiKey(context.Background())
if resp.StatusCode == http.StatusNotFound {
return nil
Expand Down
7 changes: 3 additions & 4 deletions pkg/artifactory/resource_artifactory_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"strings"

"github.com/atlassian/go-artifactory/v2/artifactory"
v1 "github.com/atlassian/go-artifactory/v2/artifactory/v1"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -116,7 +115,7 @@ func calculateFingerPrint(pemData string) (string, error) {
}

func findCertificate(d *schema.ResourceData, m interface{}) (*v1.CertificateDetails, error) {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

certs, _, err := c.V1.Security.GetCertificates(context.Background())
if err != nil {
Expand Down Expand Up @@ -168,7 +167,7 @@ func resourceCertificateRead(d *schema.ResourceData, m interface{}) error {
}

func resourceCertificateUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

_, _, err := c.V1.Security.AddCertificate(context.Background(), d.Id(), strings.NewReader(d.Get("content").(string)))
if err != nil {
Expand All @@ -179,7 +178,7 @@ func resourceCertificateUpdate(d *schema.ResourceData, m interface{}) error {
}

func resourceCertificateDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

_, _, err := c.V1.Security.DeleteCertificate(context.Background(), d.Id())
if err != nil {
Expand Down
13 changes: 6 additions & 7 deletions pkg/artifactory/resource_artifactory_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"net/http"

"github.com/atlassian/go-artifactory/v2/artifactory"
"github.com/atlassian/go-artifactory/v2/artifactory/v1"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -78,7 +77,7 @@ func unmarshalGroup(s *schema.ResourceData) (*v1.Group, error) {
}

func resourceGroupCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

group, err := unmarshalGroup(d)

Expand All @@ -94,7 +93,7 @@ func resourceGroupCreate(d *schema.ResourceData, m interface{}) error {

d.SetId(*group.Name)
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld
_, resp, err := c.V1.Security.GetGroup(context.Background(), d.Id())
if err != nil {
return resource.NonRetryableError(fmt.Errorf("error describing group: %s", err))
Expand All @@ -109,7 +108,7 @@ func resourceGroupCreate(d *schema.ResourceData, m interface{}) error {
}

func resourceGroupRead(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

group, resp, err := c.V1.Security.GetGroup(context.Background(), d.Id())

Expand Down Expand Up @@ -137,7 +136,7 @@ func resourceGroupRead(d *schema.ResourceData, m interface{}) error {
}

func resourceGroupUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld
group, err := unmarshalGroup(d)
if err != nil {
return err
Expand All @@ -152,7 +151,7 @@ func resourceGroupUpdate(d *schema.ResourceData, m interface{}) error {
}

func resourceGroupDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld
group, err := unmarshalGroup(d)
if err != nil {
return err
Expand All @@ -168,7 +167,7 @@ func resourceGroupDelete(d *schema.ResourceData, m interface{}) error {
}

func resourceGroupExists(d *schema.ResourceData, m interface{}) (bool, error) {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

groupName := d.Id()
_, resp, err := c.V1.Security.GetGroup(context.Background(), groupName)
Expand Down
10 changes: 5 additions & 5 deletions pkg/artifactory/resource_artifactory_local_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func unmarshalLocalRepository(s *schema.ResourceData) *v1.LocalRepository {
}

func resourceLocalRepositoryCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

repo := unmarshalLocalRepository(d)

Expand All @@ -192,7 +192,7 @@ func resourceLocalRepositoryCreate(d *schema.ResourceData, m interface{}) error
}

func resourceLocalRepositoryRead(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

repo, resp, err := c.V1.Repositories.GetLocal(context.Background(), d.Id())

Expand Down Expand Up @@ -236,7 +236,7 @@ func resourceLocalRepositoryRead(d *schema.ResourceData, m interface{}) error {
}

func resourceLocalRepositoryUpdate(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

repo := unmarshalLocalRepository(d)
_, err := c.V1.Repositories.UpdateLocal(context.Background(), d.Id(), repo)
Expand All @@ -250,7 +250,7 @@ func resourceLocalRepositoryUpdate(d *schema.ResourceData, m interface{}) error
}

func resourceLocalRepositoryDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld
repo := unmarshalLocalRepository(d)

resp, err := c.V1.Repositories.DeleteLocal(context.Background(), *repo.Key)
Expand All @@ -263,7 +263,7 @@ func resourceLocalRepositoryDelete(d *schema.ResourceData, m interface{}) error
}

func resourceLocalRepositoryExists(d *schema.ResourceData, m interface{}) (bool, error) {
c := m.(*artifactory.Artifactory)
c := m.(*ArtClient).ArtOld

_, resp, err := c.V1.Repositories.GetLocal(context.Background(), d.Id())

Expand Down
Loading

0 comments on commit ba711e0

Please sign in to comment.