Skip to content

Commit

Permalink
Merge pull request #235 from jfrog/GH-229-add-toggle-for-checking-lic…
Browse files Browse the repository at this point in the history
…ense

GH-229 Add check_license attribute to provider
  • Loading branch information
alexhung authored Dec 10, 2021
2 parents 2f75ebe + 211ae04 commit 3c3dabd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
7 changes: 4 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,12 @@ provider "artifactory" {
The following arguments are supported:

* `url` - (Required) URL of Artifactory. This can also be sourced from the `ARTIFACTORY_URL` environment variable.
* `username` - (Optional) Username for basic auth. Requires `password` to be set.
* `username` - (Optional) Username for basic auth. Requires `password` to be set.
Conflicts with `api_key`, and `access_token`. This can also be sourced from the `ARTIFACTORY_USERNAME` environment variable.
* `password` - (Optional) Password for basic auth. Requires `username` to be set.
* `password` - (Optional) Password for basic auth. Requires `username` to be set.
Conflicts with `api_key`, and `access_token`. This can also be sourced from the `ARTIFACTORY_PASSWORD` environment variable.
* `api_key` - (Optional) API key for api auth. Uses `X-JFrog-Art-Api` header.
* `api_key` - (Optional) API key for api auth. Uses `X-JFrog-Art-Api` header.
Conflicts with `username`, `password`, and `access_token`. This can also be sourced from the `ARTIFACTORY_API_KEY` environment variable.
* `access_token` - (Optional) API key for token auth. Uses `Authorization: Bearer` header. For xray functionality, this is the only auth method accepted
Conflicts with `username` and `password`, and `api_key`. This can also be sourced from the `ARTIFACTORY_ACCESS_TOKEN` environment variable.
* `check_license` - (Optional) Toggle for pre-flight checking of Artifactory license. Default to `true`.
18 changes: 14 additions & 4 deletions pkg/artifactory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func Provider() *schema.Provider {
ConflictsWith: []string{"api_key", "password"},
Description: "This is a bearer token that can be given to you by your admin under `Identity and Access`",
},
"check_license": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Toggle for pre-flight checking of Artifactory license. Default to `true`.",
},
},

ResourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -148,6 +154,7 @@ func buildResty(URL string) (*resty.Client, error) {
if response == nil {
return fmt.Errorf("no response found")
}

if response.StatusCode() >= http.StatusBadRequest {
return fmt.Errorf("\n%d %s %s\n%s", response.StatusCode(), response.Request.Method, response.Request.URL, string(response.Body()[:]))
}
Expand Down Expand Up @@ -196,9 +203,12 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
return nil, err
}

err = checkArtifactoryLicense(restyBase)
if err != nil {
return nil, err
checkLicense := d.Get("check_license").(bool)
if checkLicense {
err = checkArtifactoryLicense(restyBase)
if err != nil {
return nil, err
}
}

_, err = sendUsageRepo(restyBase, terraformVersion)
Expand All @@ -214,7 +224,7 @@ func providerConfigure(d *schema.ResourceData, terraformVersion string) (interfa
func checkArtifactoryLicense(client *resty.Client) error {

type License struct {
Type string `json:"type"`
Type string `json:"type"`
ValidThrough string `json:"validThrough"`
LicensedTo string `json:"licensedTo"`
}
Expand Down
6 changes: 1 addition & 5 deletions pkg/artifactory/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ func testAccPreCheck(t *testing.T) {
if err != nil {
t.Fatal(err)
}
// TODO check the payload and make sure it's the right license type
_, err = restyClient.R().Get("/artifactory/api/system/licenses/")
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
provider, _ := testAccProviders["artifactory"]()
oldErr := provider.Configure(ctx, terraform.NewResourceConfigRaw(nil))
Expand Down
24 changes: 18 additions & 6 deletions sample.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@ terraform {
required_providers {
artifactory = {
source = "registry.terraform.io/jfrog/artifactory"
version = "2.6.19"
version = "2.6.22"
}
}
}

provider "artifactory" {
// supply ARTIFACTORY_USERNAME, _PASSWORD and _URL as env vars
}

resource "artifactory_local_nuget_repository" "my-nuget-local" {
key = "my-nuget-local"
max_unique_snapshots = 10
force_nuget_authentication = true
}

resource "artifactory_local_generic_repository" "my-generic-local" {
key = "my-generic-local"
}

resource "artifactory_local_npm_repository" "my-npm-local" {
key = "my-npm-local"
}

resource "artifactory_local_maven_repository" "my-maven-local" {
key = "my-maven-local"
checksum_policy_type = "client-checksums"
Expand All @@ -43,13 +51,15 @@ resource "artifactory_local_docker_v2_repository" "foo" {
tag_retention = 3
max_unique_tags = 5
}

resource "artifactory_local_docker_v1_repository" "foo" {
key = "foo"
}

resource "random_id" "randid" {
byte_length = 16
}

resource "tls_private_key" "example" {
algorithm = "RSA"
rsa_bits = 2048
Expand All @@ -68,6 +78,7 @@ resource "artifactory_keypair" "some-keypairRSA" {
]
}
}

resource "artifactory_keypair" "some-keypairGPG1" {
pair_name = "some-keypair${random_id.randid.id}"
pair_type = "GPG"
Expand All @@ -79,8 +90,9 @@ resource "artifactory_keypair" "some-keypairGPG1" {
private_key,
passphrase,
]
}
}
}

resource "artifactory_keypair" "some-keypairGPG2" {
pair_name = "some-keypair4${random_id.randid.id}"
pair_type = "GPG"
Expand All @@ -94,6 +106,7 @@ resource "artifactory_keypair" "some-keypairGPG2" {
]
}
}

resource "artifactory_local_debian_repository" "my-debian-repo" {
key = "my-debian-repo"
primary_keypair_ref = artifactory_keypair.some-keypairGPG1.pair_name
Expand All @@ -108,6 +121,7 @@ resource "artifactory_local_alpine_repository" "terraform-local-test-repo-basic1
primary_keypair_ref = artifactory_keypair.some-keypairRSA.pair_name
depends_on = [artifactory_keypair.some-keypairRSA]
}

variable "supported_repo_types" {
type = list(string)
default = [
Expand Down Expand Up @@ -146,7 +160,6 @@ variable "supported_repo_types" {
]
}


resource "artifactory_local_repository" "local" {
count = length(var.supported_repo_types)
key = "${var.supported_repo_types[count.index]}-local"
Expand All @@ -163,9 +176,6 @@ resource "artifactory_local_repository" "local-rand" {
description = "hello ${count.index}-local"
}

provider "artifactory" {
// supply ARTIFACTORY_USERNAME, _PASSWORD and _URL as env vars
}
resource "artifactory_remote_repository" "npm-remote" {
key = "npm-remote"
package_type = "npm"
Expand All @@ -187,6 +197,7 @@ resource "artifactory_virtual_go_repository" "baz-go" {
"**/go.googlesource.com/**"
]
}

resource "artifactory_remote_npm_repository" "thing" {
key = "remote-thing-npm"
url = "https://registry.npmjs.org/"
Expand All @@ -195,6 +206,7 @@ resource "artifactory_remote_npm_repository" "thing" {
list_remote_folder_items = true
mismatching_mime_types_override_list = "application/json,application/xml"
}

resource "artifactory_virtual_maven_repository" "foo" {
key = "maven-virt-repo"
repo_layout_ref = "maven-2-default"
Expand Down

0 comments on commit 3c3dabd

Please sign in to comment.