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

Migrate all report resources #240

Merged
merged 13 commits into from
Sep 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21
go-version: '1.22.7'
- name: Install Terraform CLI
uses: hashicorp/setup-terraform@v3
if: ${{ matrix.cli == 'terraform' }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.21
go-version: '1.22.7'
-
name: Import GPG key
id: import_gpg
Expand Down
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ builds:
flags:
- -trimpath
ldflags:
- '-s -w -X github.com/jfrog/terraform-provider-xray/pkg/xray.Version={{.Version}}'
- '-s -w -X github.com/jfrog/terraform-provider-xray/pkg/xray/provider.Version={{.Version}}'
goos:
- freebsd
- windows
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## 2.11.1 (September 13, 2024). Tested on Artifactory 7.90.9 and Xray 3.104.8 with Terraform 1.9.5 and OpenTofu 1.8.2
## 2.11.1 (September 16, 2024). Tested on Artifactory 7.90.10 and Xray 3.104.11 with Terraform 1.9.5 and OpenTofu 1.8.2

IMPROVEMENTS:

* resource/xray_license_policy, resource/xray_operational_risk_policy, resource/xray_security_policy: Migrate from SDKv2 to Plugin Framework. PR: [#239](https://github.com/jfrog/terraform-provider-xray/pull/239)
* resource/xray_licenses_report, resource/xray_operational_risks_report, resource/xray_violations_report, resource/xray_vulnerabilities_report: Migrate from SDKv2 to Plugin Framework. PR: [#240](https://github.com/jfrog/terraform-provider-xray/pull/240)

## 2.11.0 (August 27, 2024). Tested on Artifactory 7.90.8 and Xray 3.102.5 with Terraform 1.9.5 and OpenTofu 1.8.1

Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ attach:

acceptance: fmt
export TF_ACC=true && \
go test -cover -coverprofile=coverage.txt -ldflags="-X '${PKG_VERSION_PATH}.Version=${NEXT_VERSION}-test'" -v -p 1 -parallel 20 -timeout 35m ./pkg/...
go test -cover -coverprofile=coverage.txt -ldflags="-X '${PKG_VERSION_PATH}/provider.Version=${NEXT_VERSION}-test'" -v -p 1 -parallel 20 -timeout 35m ./pkg/...

# To generate coverage.txt run `make acceptance` first
coverage:
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/jfrog/terraform-provider-xray
// if you need to do local dev, literally just uncomment the line below
// replace github.com/jfrog/terraform-provider-shared => ../terraform-provider-shared

go 1.21.5
go 1.22.7

require (
github.com/go-resty/resty/v2 v2.14.0
Expand All @@ -12,8 +12,6 @@ require (
github.com/hashicorp/terraform-plugin-framework v1.11.0
github.com/hashicorp/terraform-plugin-framework-validators v0.13.0
github.com/hashicorp/terraform-plugin-go v0.23.0
github.com/hashicorp/terraform-plugin-mux v0.16.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/hashicorp/terraform-plugin-testing v1.10.0
github.com/jfrog/terraform-provider-shared v1.25.5
github.com/samber/lo v1.47.0
Expand Down Expand Up @@ -55,6 +53,7 @@ require (
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.22.1 // indirect
github.com/hashicorp/terraform-plugin-log v0.9.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 // indirect
github.com/hashicorp/terraform-registry-address v0.2.3 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
Expand Down Expand Up @@ -89,8 +88,6 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
google.golang.org/protobuf v1.34.0 // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d // indirect
gopkg.in/ldap.v2 v2.5.1 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,6 @@ github.com/hashicorp/terraform-plugin-go v0.23.0 h1:AALVuU1gD1kPb48aPQUjug9Ir/12
github.com/hashicorp/terraform-plugin-go v0.23.0/go.mod h1:1E3Cr9h2vMlahWMbsSEcNrOCxovCZhOOIXjFHbjc/lQ=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
github.com/hashicorp/terraform-plugin-log v0.9.0/go.mod h1:rKL8egZQ/eXSyDqzLUuwUYLVdlYeamldAHSxjUFADow=
github.com/hashicorp/terraform-plugin-mux v0.16.0 h1:RCzXHGDYwUwwqfYYWJKBFaS3fQsWn/ZECEiW7p2023I=
github.com/hashicorp/terraform-plugin-mux v0.16.0/go.mod h1:PF79mAsPc8CpusXPfEVa4X8PtkB+ngWoiUClMrNZlYo=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 h1:kJiWGx2kiQVo97Y5IOGR4EMcZ8DtMswHhUuFibsCQQE=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0/go.mod h1:sl/UoabMc37HA6ICVMmGO+/0wofkVIRxf+BMb/dnoIg=
github.com/hashicorp/terraform-plugin-testing v1.10.0 h1:2+tmRNhvnfE4Bs8rB6v58S/VpqzGC6RCh9Y8ujdn+aw=
Expand Down Expand Up @@ -323,13 +321,9 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d h1:TxyelI5cVkbREznMhfzycHdkp5cLA7DpE+GKjSslYhM=
gopkg.in/asn1-ber.v1 v1.0.0-20181015200546-f715ec2f112d/go.mod h1:cuepJuh7vyXfUyUwEgHQXw849cJrilpS5NeIjOWESAw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ldap.v2 v2.5.1 h1:wiu0okdNfjlBzg6UWvd1Hn8Y+Ux17/u/4nlk4CQr6tU=
gopkg.in/ldap.v2 v2.5.1/go.mod h1:oI0cpe/D7HRtBQl8aTg+ZmzFUAvu4lsv3eLXMLGFxWk=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
46 changes: 6 additions & 40 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,26 @@ import (
"log"

"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/jfrog/terraform-provider-xray/pkg/xray/provider"
"github.com/jfrog/terraform-provider-xray/pkg/xray"
)

// Run the docs generation tool, check its repository for more information on how it works and how docs
// can be customized.
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs

func main() {
ctx := context.Background()

var debug bool

flag.BoolVar(&debug, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

upgradedSdkServer, err := tf5to6server.UpgradeServer(
ctx,
provider.SdkV2().GRPCProvider, // Example terraform-plugin-sdk provider
)

if err != nil {
log.Fatal(err)
opts := providerserver.ServeOpts{
Address: "registry.terraform.io/jfrog/xray",
Debug: debug,
}

providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(provider.Framework()()), // Example terraform-plugin-framework provider
func() tfprotov6.ProviderServer {
return upgradedSdkServer
},
}

muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)

if err != nil {
log.Fatal(err)
}

var serveOpts []tf6server.ServeOpt

if debug {
serveOpts = append(serveOpts, tf6server.WithManagedDebug())
}

err = tf6server.Serve(
"registry.terraform.io/jfrog/xray",
muxServer.ProviderServer,
serveOpts...,
)

err := providerserver.Serve(context.Background(), xray.NewProvider(), opts)
if err != nil {
log.Fatal(err)
log.Fatal(err.Error())
}
}
79 changes: 9 additions & 70 deletions pkg/acctest/test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,36 @@
package acctest

import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"testing"

"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
terraform2 "github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/jfrog/terraform-provider-shared/client"
"github.com/jfrog/terraform-provider-shared/testutil"
"github.com/jfrog/terraform-provider-shared/util"
"github.com/jfrog/terraform-provider-xray/pkg/xray/provider"
"github.com/jfrog/terraform-provider-xray/pkg/xray"
)

// Provider PreCheck(t) must be called before using this provider instance.
var Provider *schema.Provider
var ProviderFactories map[string]func() (*schema.Provider, error)

// testAccProviderConfigure ensures Provider is only configured once
//
// The PreCheck(t) function is invoked for every test and this prevents
// extraneous reconfiguration to the same values each time. However, this does
// not prevent reconfiguration that may happen should the address of
// Provider be errantly reused in ProviderFactories.
var testAccProviderConfigure sync.Once

// ProtoV6MuxProviderFactories is used to instantiate both SDK v2 and Framework providers
// during acceptance tests. Use it only if you need to combine resources from SDK v2 and the Framework in the same test.
var ProtoV6MuxProviderFactories map[string]func() (tfprotov6.ProviderServer, error)
var Provider provider.Provider

var ProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"xray": providerserver.NewProtocol6WithError(provider.Framework()()),
"xray": providerserver.NewProtocol6WithError(xray.NewProvider()()),
}

func init() {
Provider = provider.SdkV2()

ProviderFactories = map[string]func() (*schema.Provider, error){
"xray": func() (*schema.Provider, error) { return Provider, nil },
}

ProtoV6MuxProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"xray": func() (tfprotov6.ProviderServer, error) {
ctx := context.Background()

upgradedSdkServer, err := tf5to6server.UpgradeServer(
ctx,
provider.SdkV2().GRPCProvider, // terraform-plugin-sdk provider
)
if err != nil {
return nil, err
}

providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(provider.Framework()()), // terraform-plugin-framework provider
func() tfprotov6.ProviderServer {
return upgradedSdkServer
},
}
Provider = xray.NewProvider()()

muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)

if err != nil {
return nil, err
}

return muxServer.ProviderServer(), nil
},
ProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"xray": providerserver.NewProtocol6WithError(Provider),
}
}

// PreCheck This function should be present in every acceptance test.
func PreCheck(t *testing.T) {
// Since we are outside the scope of the Terraform configuration we must
// call Configure() to properly initialize the provider configuration.
testAccProviderConfigure.Do(func() {
configErr := Provider.Configure(context.Background(), (*terraform2.ResourceConfig)(terraform2.NewResourceConfigRaw(nil)))
if configErr != nil && configErr.HasError() {
t.Fatalf("failed to configure provider %v", configErr)
}
})
}

type CheckFun func(id string, request *resty.Request) (*resty.Response, error)

func VerifyDeleted(id, identifierAttribute string, check CheckFun) func(*terraform.State) error {
Expand All @@ -104,14 +44,13 @@ func VerifyDeleted(id, identifierAttribute string, check CheckFun) func(*terrafo
return fmt.Errorf("provider is not initialized. Please PreCheck() is included in your acceptance test")
}

providerMeta := Provider.Meta().(util.ProviderMetadata)

identifier := rs.Primary.ID
if identifierAttribute != "" {
identifier = rs.Primary.Attributes[identifierAttribute]
}

resp, err := check(identifier, providerMeta.Client.R())
client := Provider.(*xray.XrayProvider).Meta.Client
resp, err := check(identifier, client.R())
if err != nil {
return err
}
Expand Down
26 changes: 17 additions & 9 deletions pkg/xray/provider/framework.go → pkg/xray/provider.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider
package xray

import (
"context"
Expand All @@ -18,10 +18,15 @@ import (
xray_resource "github.com/jfrog/terraform-provider-xray/pkg/xray/resource"
)

var Version = "2.11.1"
var productId = "terraform-provider-xray/" + Version

// Ensure the implementation satisfies the provider.Provider interface.
var _ provider.Provider = &XrayProvider{}

type XrayProvider struct{}
type XrayProvider struct {
Meta util.ProviderMetadata
}

// XrayProviderModel describes the provider data model.
type XrayProviderModel struct {
Expand Down Expand Up @@ -159,17 +164,16 @@ func (p *XrayProvider) Configure(ctx context.Context, req provider.ConfigureRequ
featureUsage := fmt.Sprintf("Terraform/%s", req.TerraformVersion)
go util.SendUsage(ctx, restyClient.R(), productId, featureUsage)

resp.DataSourceData = util.ProviderMetadata{
meta := util.ProviderMetadata{
Client: restyClient,
ProductId: productId,
XrayVersion: version,
}

resp.ResourceData = util.ProviderMetadata{
Client: restyClient,
ProductId: productId,
XrayVersion: version,
}
p.Meta = meta

resp.DataSourceData = meta
resp.ResourceData = meta
}

// Resources satisfies the provider.Provider interface for ArtifactoryProvider.
Expand All @@ -181,10 +185,14 @@ func (p *XrayProvider) Resources(ctx context.Context) []func() resource.Resource
xray_resource.NewCustomIssueResource,
xray_resource.NewIgnoreRuleResource,
xray_resource.NewLicensePolicyResource,
xray_resource.NewLicensesReportResource,
xray_resource.NewOperationalRiskPolicyResource,
xray_resource.NewOperationalRisksReportResource,
xray_resource.NewRepositoryConfigResource,
xray_resource.NewSecurityPolicyResource,
xray_resource.NewSettingsResource,
xray_resource.NewViolationsReportResource,
xray_resource.NewVulnerabilitiesReportResource,
xray_resource.NewWatchResource,
xray_resource.NewWebhookResource,
xray_resource.NewWorkersCountResource,
Expand All @@ -198,7 +206,7 @@ func (p *XrayProvider) DataSources(_ context.Context) []func() datasource.DataSo
}
}

func Framework() func() provider.Provider {
func NewProvider() func() provider.Provider {
return func() provider.Provider {
return &XrayProvider{}
}
Expand Down
Loading
Loading