diff --git a/GNUmakefile b/GNUmakefile new file mode 100644 index 0000000..98cfb90 --- /dev/null +++ b/GNUmakefile @@ -0,0 +1,39 @@ +TEST?=$$(go list ./... |grep -v 'vendor') +TEST_PARALLELISM?=4 +GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) +PKG_NAME=cors + +default: build + +build: + go install $(FLAGS) + +test: + go test -i $(TEST) || exit 1 + echo $(TEST) | \ + xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=$(TEST_PARALLELISM) + +testacc: + TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 360m -parallel $(TEST_PARALLELISM) + +vet: + @echo "go vet ." + @go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \ + echo ""; \ + echo "Vet found suspicious constructs. Please check the reported constructs"; \ + echo "and fix them if necessary before submitting the code for review."; \ + exit 1; \ + fi + +fmt: + gofmt -w $(GOFMT_FILES) + +test-compile: + @if [ "$(TEST)" = "./..." ]; then \ + echo "ERROR: Set TEST to a specific package. For example,"; \ + echo " make test-compile TEST=./$(PKG_NAME)"; \ + exit 1; \ + fi + go test -c $(TEST) $(TESTARGS) + +.PHONY: build test testacc vet fmt test-compile diff --git a/README.md b/README.md new file mode 100644 index 0000000..5239972 --- /dev/null +++ b/README.md @@ -0,0 +1,78 @@ +Terraform HuaweiCloud Provider +============================== + + +* Website: https://www.terraform.io +* [![Documentation](https://img.shields.io/badge/documentation-blue)](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs) +* [![Gitter chat](https://badges.gitter.im/hashicorp-terraform/Lobby.png)](https://gitter.im/hashicorp-terraform/Lobby) +* Mailing list: [Google Groups](http://groups.google.com/group/terraform-tool) + + + + +Requirements +------------ + +* [Terraform](https://www.terraform.io/downloads.html) 0.12.x +* [Go](https://golang.org/doc/install) 1.18 (to build the provider plugin) + +Building The Provider +--------------------- + +Clone repository to: `$GOPATH/src/github.com/huaweicloud/terraform-provider-huaweicloud` + +```sh +$ mkdir -p $GOPATH/src/github.com/huaweicloud; cd $GOPATH/src/github.com/huaweicloud +$ git clone https://github.com/huaweicloud/terraform-provider-huaweicloud +``` + +Enter the provider directory and build the provider + +```sh +$ cd $GOPATH/src/github.com/huaweicloud/terraform-provider-huaweicloud +$ make build +``` + +Using the provider +------------------ + +Please see the documentation at [provider usage](docs/index.md). + +Or you can browse the documentation within this repo [here](https://github.com/huaweicloud/terraform-provider-huaweicloud/tree/master/docs). + +Developing the Provider +----------------------- + +If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed +on your machine (version 1.14+ is *required*). +You'll also need to correctly setup a [GOPATH](http://golang.org/doc/code.html#GOPATH), +as well as adding `$GOPATH/bin` to your `$PATH`. + +To compile the provider, run `make build`. +This will build the provider and put the provider binary in the `$GOPATH/bin` directory. + +```sh +$ make build +... +$ $GOPATH/bin/terraform-provider-huaweicloud +... +``` + +In order to test the provider, you can simply run `make test`. + +```sh +$ make test +``` + +In order to run the full suite of Acceptance tests, run `make testacc`. + +*Note:* Acceptance tests create real resources, and often cost money to run. + +```sh +$ make testacc +``` + +License +------- + +Terraform-Provider-Huaweicloud is under the Mozilla Public License 2.0. See the [LICENSE](LICENSE) file for details. diff --git a/cors/config/config.go b/cors/config/config.go new file mode 100644 index 0000000..035aa1b --- /dev/null +++ b/cors/config/config.go @@ -0,0 +1,9 @@ +package config + +type Config struct { + Region string + DomainID string + DomainID string + DomainID string + MaxRetries int +} diff --git a/cors/provider.go b/cors/provider.go new file mode 100644 index 0000000..c8fc5fc --- /dev/null +++ b/cors/provider.go @@ -0,0 +1,71 @@ +package cors + +import ( + "context" + + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/chnsz/terraform-provider-cors/cors/config/config" + //"github.com/chnsz/terraform-provider-cors/cors/services/cbr" +) + +// Provider returns a schema.Provider for CORS. +func Provider() *schema.Provider { + provider := &schema.Provider{ + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("CORS_REGION", nil), + }, + + "domain_id": { + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("CORS_DOMAIN_ID", nil), + }, + + "project_id": { + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("CORS_PROJECT_ID", nil), + }, + + "app_code": { + Type: schema.TypeString, + Required: true, + DefaultFunc: schema.EnvDefaultFunc("CORS_APP_CODE", nil), + }, + + "max_retries": { + Type: schema.TypeInt, + Optional: true, + DefaultFunc: schema.EnvDefaultFunc("CORS_MAX_RETRIES", 5), + }, + }, + + DataSourcesMap: map[string]*schema.Resource{ + //"huaweicloud_cbr_vaults": cbr.DataSourceCbrVaults(), + }, + + ResourcesMap: map[string]*schema.Resource{ + //"huaweicloud_cbr_vault": cbr.ResourceCbrVault(), + }, + ConfigureContextFunc = configProvider + } + + return provider +} + +func configProvider(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { + config := config.Config{ + Region: d.Get("region").(string), + DomainID: d.Get("domain_id").(string), + ProjectID: d.Get("project_id").(string), + AppCode: d.Get("app_code").(string), + MaxRetries: d.Get("max_retries").(int), + } + + return &config, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..39a528e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/chnsz/terraform-provider-cors + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..201c301 --- /dev/null +++ b/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "log" + + "github.com/hashicorp/terraform-plugin-sdk/v2/plugin" + "github.com/chnsz/terraform-provider-cors/cors" +) + +func main() { + // Remove any date and time prefix in log package function output to + // prevent duplicate timestamp and incorrect log level setting + log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime)) + + plugin.Serve(&plugin.ServeOpts{ + ProviderFunc: cors.Provider}) +}