diff --git a/.chglog/CHANGELOG.tpl.md b/.chglog/CHANGELOG.tpl.md new file mode 100644 index 00000000..1392af52 --- /dev/null +++ b/.chglog/CHANGELOG.tpl.md @@ -0,0 +1,34 @@ +{{ if .Versions -}} +{{ if .Unreleased.CommitGroups }} + +## [Unreleased]({{ .Info.RepositoryURL }}/compare/{{ $latest := index .Versions 0 }}{{ $latest.Tag.Name }}...HEAD) +{{ range .Unreleased.CommitGroups -}} +### {{ .Title }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} +{{ range .Unreleased.NoteGroups -}} +### {{ .Title }} +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ range .Versions }} + +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }}) +{{ range .CommitGroups -}} +### {{ .Title }} +{{ range .Commits -}} +- {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} +{{ range .NoteGroups -}} +### {{ .Title }} +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} \ No newline at end of file diff --git a/.chglog/config.yml b/.chglog/config.yml new file mode 100644 index 00000000..5f6f2248 --- /dev/null +++ b/.chglog/config.yml @@ -0,0 +1,28 @@ +style: github +template: CHANGELOG.tpl.md +info: + title: CHANGELOG + repository_url: https://github.com/taliesins/terraform-provider-hyperv +options: + commits: + # filters: + # Type: + # - feat + # - fix + # - perf + # - refactor + commit_groups: + # title_maps: + # feat: Features + # fix: Bug Fixes + # perf: Performance Improvements + # refactor: Code Refactoring + header: + pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" + pattern_maps: + - Type + - Scope + - Subject + notes: + keywords: + - BREAKING CHANGE \ No newline at end of file diff --git a/.github/CLEANCHANGELOG.md b/.github/CLEANCHANGELOG.md deleted file mode 100644 index 8b137891..00000000 --- a/.github/CLEANCHANGELOG.md +++ /dev/null @@ -1 +0,0 @@ - diff --git a/.github/workflows/create-release.yml b/.github/workflows/create-release.yml index 844e7133..8c162612 100644 --- a/.github/workflows/create-release.yml +++ b/.github/workflows/create-release.yml @@ -3,6 +3,10 @@ name: Create tag on: workflow_dispatch: +env: + GIT_CHGLOG_VERSION: "0.15.4" + SVU_VERSION: "1.12.0" + jobs: build: runs-on: ubuntu-latest @@ -12,35 +16,34 @@ jobs: # Fetches entire history, so we can analyze commits since last tag fetch-depth: 0 persist-credentials: false - - - name: Create Changelog - id: release_number_and_changelog - uses: taliesins/conventional-changelog-action@releases/v3 + - uses: actions/setup-git-chglog@v1 with: - github-token: ${{ secrets.CREATE_TAG_GITHUB_TOKEN }} - skip-version-file: 'true' - skip-on-empty: 'false' - skip-commit: 'true' - skip-tag: 'true' - dry-run: 'true' - tag-prefix: 'v' - output-file: 'false' - - - name: Create clean changelog - uses: "finnp/create-file-action@master" - env: - FILE_NAME: ".github/CLEANCHANGELOG.md" - FILE_DATA: ${{ steps.release_number_and_changelog.outputs.clean_changelog }} - - - name: Create Changelog and commit - uses: taliesins/conventional-changelog-action@releases/v3 + git-chglog-version: ${{ env.GIT_CHGLOG_VERSION}} + - uses: obfu5c8/action-svu + id: install_svu + with: + type: none + svu-version: ${{ env.SVU_VERSION}} + - name: get version + id: get_version + run: | + current_version="$(svu current --tag-mode current-branch --strip-prefix)" + prerelease_version="$(svu prerelease --tag-mode all-branches --strip-prefix --pre-release prerelease)" + release_version="$(svu next --tag-mode all-branches --strip-prefix)" + + echo ::set-output name=CURRENT_VERSION::${current_version} + echo ::set-output name=PRERELEASE_VERSION::${prerelease_version} + echo ::set-output name=RELEASE_VERSION::${release_version} + + echo "current version: ${current_version}" + echo "prerelease version: ${prerelease_version}" + echo "release version: ${release_version}" + - name: generate change log for release + run: | + git-chglog --next-tag "v${{ steps.get_version.outputs.RELEASE_VERSION }}" > CHANGELOG.md + - name: Add updated changelog + uses: EndBug/add-and-commit@v9 with: - github-token: ${{ secrets.CREATE_TAG_GITHUB_TOKEN }} - skip-version-file: 'true' - skip-on-empty: 'false' - skip-commit: 'false' - skip-tag: 'false' - dry-run: 'false' - force-push: 'true' - tag-prefix: 'v' - output-file: 'CHANGELOG.md' \ No newline at end of file + default_author: github_actions + message: "chore(release): v${{ steps.get_version.outputs.RELEASE_VERSION }}" + add: "CHANGELOG.md" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fe52e6e3..4abe19f1 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -18,7 +18,6 @@ on: - 'v*' paths-ignore: - 'CHANGELOG.md' - - '.github/CLEANCHANGELOG.md' pull_request: paths: - .github/workflows/release.yml @@ -37,9 +36,11 @@ on: - website/** env: - GO_VERSION: "1.19.4" + GO_VERSION: "1.21.5" GO111MODULE: on - TERRAFORM_VERSION: "1.3.6" + TERRAFORM_VERSION: "1.5.5" + GIT_CHGLOG_VERSION: "0.15.4" + SVU_VERSION: "1.12.0" TEST_TIME_OUT: "120s" RELEASE_TIME_OUT: "30m" @@ -222,21 +223,60 @@ jobs: - run: golangci-lint run semgrep: + # User definable name of this GitHub Actions job. + name: semgrep/ci + # If you are self-hosting, change the following `runs-on` value: runs-on: ubuntu-latest + + container: + # A Docker image with Semgrep installed. Do not change this. + image: returntocorp/semgrep + + # Skip any PR created by dependabot to avoid permission issues: + if: (github.actor != 'dependabot[bot]') + steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: semgrep - uses: returntocorp/semgrep-action@v1 + # Fetch project source with GitHub Actions Checkout. + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + # Run the "semgrep ci" command on the command line of the docker image. + - run: semgrep ci --no-suppress-errors + env: + # Connect to Semgrep Cloud Platform through your SEMGREP_APP_TOKEN. + # Generate a token from Semgrep Cloud Platform > Settings + # and add it to your GitHub secrets. + SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} goreleaser: needs: [go_mod_download] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 + - uses: actions/setup-git-chglog@v1 + with: + git-chglog-version: ${{ env.GIT_CHGLOG_VERSION}} + - uses: obfu5c8/action-svu + id: install_svu + with: + type: none + svu-version: ${{ env.SVU_VERSION}} + - name: get version + id: get_version + run: | + current_version="$(svu current --tag-mode current-branch --strip-prefix)" + prerelease_version="$(svu prerelease --tag-mode all-branches --strip-prefix --pre-release prerelease)" + release_version="$(svu next --tag-mode all-branches --strip-prefix)" + + echo ::set-output name=CURRENT_VERSION::${current_version} + echo ::set-output name=PRERELEASE_VERSION::${prerelease_version} + echo ::set-output name=RELEASE_VERSION::${release_version} + + echo "current version: ${current_version}" + echo "prerelease version: ${prerelease_version}" + echo "release version: ${release_version}" - uses: actions/setup-go@v4 with: go-version: ${{ env.GO_VERSION }} @@ -248,7 +288,7 @@ jobs: key: ${{ hashFiles('.github/workflows/release.yml') }}-${{ runner.os }}-go-pkg-mod-${{ hashFiles('go.sum') }} - name: goreleaser check continue-on-error: true - uses: goreleaser/goreleaser-action@v4.2.0 + uses: goreleaser/goreleaser-action@v5 with: args: check - name: Import GPG key @@ -257,66 +297,30 @@ jobs: if: startsWith(github.ref, 'refs/tags/v') with: gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} - #passphrase: ${{ secrets.PASSPHRASE }} - - name: Get tag_name - id: tag_name - if: startsWith(github.ref, 'refs/tags/v') - run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} + #passphrase: ${{ secrets.PASSPHRASE }} - name: goreleaser release - uses: goreleaser/goreleaser-action@v4.2.0 + uses: goreleaser/goreleaser-action@v5 if: startsWith(github.ref, 'refs/tags/v') with: version: latest - args: release --rm-dist --timeout ${{ env.RELEASE_TIME_OUT }} --release-notes=.github/CLEANCHANGELOG.md + args: release --rm-dist --timeout ${{ env.RELEASE_TIME_OUT }} --release-notes=CHANGELOG.md env: GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GORELEASER_CURRENT_TAG: ${{ steps.tag_name.outputs.VERSION }} - - name: Create Changelog - id: release_number_and_changelog - uses: taliesins/conventional-changelog-action@releases/v3 - if: "!(startsWith(github.ref, 'refs/tags/v'))" - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - skip-version-file: 'true' - skip-on-empty: 'false' - skip-commit: 'true' - skip-tag: 'true' - dry-run: 'true' - tag-prefix: 'v' - output-file: 'CHANGELOG.md' - - name: Create clean changelog - uses: "finnp/create-file-action@master" - if: "!(startsWith(github.ref, 'refs/tags/v'))" - env: - FILE_NAME: ".github/CLEANCHANGELOG.md" - FILE_DATA: ${{ steps.release_number_and_changelog.outputs.clean_changelog }} - - - name: Get release information + GORELEASER_CURRENT_TAG: ${{ steps.get_version.outputs.RELEASE_VERSION }} + GORELEASER_PREVIOUS_TAG: ${{ steps.get_version.outputs.CURRENT_VERSION }} + - name: generate change log for prerelease if: "!(startsWith(github.ref, 'refs/tags/v'))" - env: - CLEAN_CHANGELOG: ${{ steps.release_number_and_changelog.outputs.clean_changelog }} - CHANGELOG: ${{ steps.release_number_and_changelog.outputs.changelog }} - VERSION: ${{ steps.release_number_and_changelog.outputs.version }} - OLDVERSION: ${{ steps.release_number_and_changelog.outputs.old_version }} - TAG: ${{ steps.release_number_and_changelog.outputs.tag }} - SKIPPED: ${{ steps.release_number_and_changelog.outputs.skipped }} run: | - echo "clean_changelog: $CLEAN_CHANGELOG" - echo "changelog: $CHANGELOG" - echo "version: $VERSION" - echo "old_version: $OLDVERSION" - echo "tag: $TAG" - echo "skipped: $SKIPPED" - + git-chglog --next-tag "v${{ steps.get_version.outputs.PRERELEASE_VERSION }}" > CHANGELOG.md - name: goreleaser snapshot - uses: goreleaser/goreleaser-action@v4.2.0 + uses: goreleaser/goreleaser-action@v5 if: "!(startsWith(github.ref, 'refs/tags/v'))" with: - args: release --rm-dist --skip-sign --snapshot --timeout ${{ env.RELEASE_TIME_OUT }} --release-notes=.github/CLEANCHANGELOG.md + args: release --rm-dist --skip-sign --snapshot --timeout ${{ env.RELEASE_TIME_OUT }} --release-notes=CHANGELOG.md env: - GORELEASER_CURRENT_TAG: v0.0.0 - GORELEASER_PREVIOUS_TAG: v0.0.0 + GORELEASER_CURRENT_TAG: ${{ steps.get_version.outputs.RELEASE_VERSION }} + GORELEASER_PREVIOUS_TAG: ${{ steps.get_version.outputs.CURRENT_VERSION }} - name: snapshot artifact naming id: naming if: "!(startsWith(github.ref, 'refs/tags/v'))" @@ -330,10 +334,18 @@ jobs: *) ARTIFACT="${GITHUB_REF}";; esac - echo "::set-output name=artifact::$ARTIFACT-$(date -u +'%Y-%m-%dT%H-%M')" + echo "::set-output name=ARTIFACT::$ARTIFACT-$(date -u +'%Y-%m-%dT%H-%M')" - name: upload snapshot artifact uses: actions/upload-artifact@v3 if: "!(startsWith(github.ref, 'refs/tags/v'))" with: - name: ${{steps.naming.outputs.artifact}} - path: dist/*.zip \ No newline at end of file + name: ${{steps.naming.outputs.ARTIFACT}} + path: dist/*.zip + - name: Add updated changelog, and create new branch + uses: EndBug/add-and-commit@v9 + if: "!(startsWith(github.ref, 'refs/tags/v'))" + with: + default_author: github_actions + message: "chore(release): v${{ steps.get_version.outputs.PRERELEASE_VERSION }}" + add: "CHANGELOG.md" + new_branch: "v${{ steps.get_version.outputs.PRERELEASE_VERSION }}" diff --git a/.gitignore b/.gitignore index 7c17f98b..08503a7e 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,9 @@ website/vendor *.winfile eol=crlf /vendor /act +/node_modules +/package-lock.json +/package.json +/.vs +/.idea +/.terraform diff --git a/.golangci.yml b/.golangci.yml index 062eaeb1..7f425316 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -28,7 +28,7 @@ linters: #- funlen #- gochecknoinits #needed for document generation - goconst - #- gocritic + - gocritic #- gocyclo - gofmt - goimports @@ -54,6 +54,14 @@ linters: linters-settings: errcheck: ignore: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema:ForceNew|Set,fmt:.*,io:Close + depguard: + rules: + logger: + deny: + # logging is allowed only by logutils.Log, + # logrus is allowed to use only in logutils package. + - pkg: "github.com/sirupsen/logrus" + desc: logging is allowed only by logutils.Log run: modules-download-mode: mod diff --git a/CHANGELOG.md b/CHANGELOG.md index a363069a..b1d0fd6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,26 +1,74 @@ -## [1.0.4](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.3...v1.0.4) (2022-12-14) - - - -## [1.0.3](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.2...v1.0.3) (2021-03-20) - - -### Bug Fixes - -* **static-or-dynamic:** don't default to static, but rather force people to chose static over dynamic. ([da10a53](https://github.com/taliesins/terraform-provider-hyperv/commit/da10a536792f8f3cecba6c988c011eff65866812)) -* improve error message to make it obvious what to do when this error occurs ([da23bf6](https://github.com/taliesins/terraform-provider-hyperv/commit/da23bf6c7acde50fbe3bc14b2a7d5a4f0b96f3e1)) - - - -## [1.0.2](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.1...v1.0.2) (2021-03-09) - - - -## [1.0.1](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.0...v1.0.1) (2020-09-23) - - - -# 1.0.0 (2020-09-23) - + +## [v1.1.0-prerelease.0](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.4...v1.1.0-prerelease.0) (2023-12-26) +### Build +- add semantic release + +### Chore +- **deps:** bump golang.org/x/net from 0.7.0 to 0.17.0 +- **deps:** bump amannn/action-semantic-pull-request +- **deps:** bump actions/setup-go from 3 to 4 +- **deps:** bump actions/cache from 3.2.1 to 3.3.1 +- **deps:** bump golang.org/x/net from 0.4.0 to 0.7.0 +- **deps:** bump goreleaser/goreleaser-action from 4.1.0 to 4.2.0 +- **deps:** bump actions/cache from 3.0.11 to 3.2.1 + +### Feat +- fix build + + + +## [v1.0.4](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.3...v1.0.4) (2022-12-14) +### Chore +- **deps:** bump actions/cache from 3.0.8 to 3.0.10 +- **deps:** bump github.com/hashicorp/terraform-plugin-sdk/v2 +- **deps:** bump github.com/hashicorp/terraform-plugin-sdk/v2 +- **deps:** bump hashicorp/setup-terraform from 2.0.2 to 2.0.3 +- **deps:** bump crazy-max/ghaction-import-gpg from 5.1.0 to 5.2.0 +- **deps:** bump goreleaser/goreleaser-action from 3.1.0 to 3.2.0 +- **deps:** bump amannn/action-semantic-pull-request +- **deps:** bump actions/cache from 3.0.10 to 3.0.11 +- **deps:** bump actions/cache from 3.0.2 to 3.0.8 +- **deps:** bump amannn/action-semantic-pull-request +- **deps:** bump actions/upload-artifact from 2 to 3 +- **deps:** bump goreleaser/goreleaser-action from 3.2.0 to 4.1.0 +- **deps:** bump hashicorp/setup-terraform from 2.0.0 to 2.0.2 +- **deps:** bump goreleaser/goreleaser-action from 3.0.0 to 3.1.0 +- **deps:** bump github.com/hashicorp/terraform-plugin-docs +- **deps:** bump crazy-max/ghaction-import-gpg from 4.3.0 to 5.1.0 +- **deps:** bump goreleaser/goreleaser-action from 2.9.1 to 3.0.0 +- **deps:** bump amannn/action-semantic-pull-request +- **deps:** bump amannn/action-semantic-pull-request +- **deps:** bump github.com/jolestar/go-commons-pool/v2 +- **deps:** bump hashicorp/setup-terraform from 1.4.0 to 2.0.0 +- **release:** v1.0.4 + + + +## [v1.0.3](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.2...v1.0.3) (2021-03-20) +### Chore +- **release:** v1.0.3 + +### Fix +- improve error message to make it obvious what to do when this error occurs +- **static-or-dynamic:** use schema to ensure exclusive selection of dynamic or static memory +- **static-or-dynamic:** don't default to static, but rather force people to chose static over dynamic. + + + +## [v1.0.2](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.1...v1.0.2) (2021-03-09) +### Chore +- **release:** v1.0.2 + +### Ci +- do not persist credentials + + + +## [v1.0.1](https://github.com/taliesins/terraform-provider-hyperv/compare/v1.0.0...v1.0.1) (2020-09-23) + + +## v1.0.0 (2020-09-23) +### Bugfix +- IsValid() -> !IsValid() diff --git a/api/hyperv-winrm/vm_firmware.go b/api/hyperv-winrm/vm_firmware.go index a74722eb..de05373a 100644 --- a/api/hyperv-winrm/vm_firmware.go +++ b/api/hyperv-winrm/vm_firmware.go @@ -172,7 +172,7 @@ func (c *ClientConfig) CreateOrUpdateVmFirmwares(ctx context.Context, vmName str return nil } if len(vmFirmwares) > 1 { - return fmt.Errorf("Only 1 vm firmware setting allowed per a vm") + return fmt.Errorf("only 1 vm firmware setting allowed per a vm") } vmFirmware := vmFirmwares[0] diff --git a/api/hyperv-winrm/vm_processor.go b/api/hyperv-winrm/vm_processor.go index 19faef46..0b7636bc 100644 --- a/api/hyperv-winrm/vm_processor.go +++ b/api/hyperv-winrm/vm_processor.go @@ -131,7 +131,7 @@ func (c *ClientConfig) CreateOrUpdateVmProcessors(ctx context.Context, vmName st return nil } if len(vmProcessors) > 1 { - return fmt.Errorf("Only 1 vm processor setting allowed per a vm") + return fmt.Errorf("only 1 vm processor setting allowed per a vm") } vmProcessor := vmProcessors[0] diff --git a/internal/provider/config.go b/internal/provider/config.go index 2034c819..291dc31a 100644 --- a/internal/provider/config.go +++ b/internal/provider/config.go @@ -54,19 +54,15 @@ func (c *Config) Client() (comm api.Client, err error) { " Password: %t\n"+ " HTTPS: %t\n"+ " Insecure: %t\n"+ - " NTLM: %t\n"+ - " KrbRealm: %s\n"+ " KrbSpn: %s\n"+ " KrbConfig: %s\n"+ " KrbCCache: %s\n"+ - " TLSServerName: %s\n"+ " CACert: %t\n"+ " Cert: %t\n"+ " Key: %t\n"+ - " ScriptPath: %s\n"+ " Timeout: %s", c.Host, @@ -126,10 +122,8 @@ func GetWinrmClient(config *Config) (winrmClient *winrm.Client, err error) { KrbCCache: config.KrbCCache, } } - } else { - if config.NTLM { - params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} } - } + } else if config.NTLM { + params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} } } if endpoint.Timeout.Seconds() > 0 { diff --git a/internal/provider/data_source_hyperv_machine_instance.go b/internal/provider/data_source_hyperv_machine_instance.go index 017ba8b2..ed26219d 100644 --- a/internal/provider/data_source_hyperv_machine_instance.go +++ b/internal/provider/data_source_hyperv_machine_instance.go @@ -34,7 +34,7 @@ func dataSourceHyperVMachineInstance() *schema.Resource { return true } - //When specifying path on new-vm it will auto append machine name on the end + // When specifying path on new-vm it will auto append machine name on the end name := d.Get("name").(string) computedPath := newValue if !strings.HasSuffix(computedPath, "\\") { @@ -793,7 +793,7 @@ func dataSourceHyperVMachineInstance() *schema.Resource { return true } - //When specifying path on new-vm it will auto append machine name on the end + // When specifying path on new-vm it will auto append machine name on the end name := d.Get("name").(string) computedPath := newValue if !strings.HasSuffix(computedPath, "\\") { diff --git a/internal/provider/data_source_hyperv_network_switch.go b/internal/provider/data_source_hyperv_network_switch.go index 55c9afaa..52fcfd1e 100644 --- a/internal/provider/data_source_hyperv_network_switch.go +++ b/internal/provider/data_source_hyperv_network_switch.go @@ -149,7 +149,8 @@ func datasourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceDa return nil } - if s.SwitchType == api.VMSwitchType_Private { + switch s.SwitchType { + case api.VMSwitchType_Private: if s.AllowManagementOS { return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to true if switch type is private") } @@ -157,7 +158,7 @@ func datasourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceDa if len(s.NetAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is private") } - } else if s.SwitchType == api.VMSwitchType_Internal { + case api.VMSwitchType_Internal: if !s.AllowManagementOS { return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to false if switch type is internal") } @@ -165,27 +166,28 @@ func datasourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceDa if len(s.NetAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is internal") } - } else if s.SwitchType == api.VMSwitchType_External { + case api.VMSwitchType_External: if len(s.NetAdapterNames) < 1 { return diag.Errorf("[ERROR][hyperv][read] Must specify NetAdapterNames if switch type is external") } } - if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute { + switch { + case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute: if s.DefaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is absolute") } if s.DefaultFlowMinimumBandwidthAbsolute < 0 { return diag.Errorf("[ERROR][hyperv][read] Bandwidth absolute must be 0 or greater") } - } else if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)) { + case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)): if s.DefaultFlowMinimumBandwidthAbsolute != 0 { return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthAbsolute should be 0 if bandwidth reservation mode is weight") } if s.DefaultFlowMinimumBandwidthWeight < 1 || s.DefaultFlowMinimumBandwidthWeight > 100 { return diag.Errorf("[ERROR][hyperv][read] Bandwidth weight must be between 1 and 100") } - } else { + default: if s.DefaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is none") } diff --git a/internal/provider/resource_hyperv_machine_instance.go b/internal/provider/resource_hyperv_machine_instance.go index a5c8cde3..de3b2a62 100644 --- a/internal/provider/resource_hyperv_machine_instance.go +++ b/internal/provider/resource_hyperv_machine_instance.go @@ -828,7 +828,7 @@ func resourceHyperVMachineInstance() *schema.Resource { return true } - //When specifying path on new-vm it will auto append machine name on the end + // When specifying path on new-vm it will auto append machine name on the end name := d.Get("name").(string) computedPath := newValue if !strings.HasSuffix(computedPath, "\\") { @@ -948,7 +948,7 @@ func resourceHyperVMachineInstanceCreate(ctx context.Context, d *schema.Resource } if existing.Exists { - return diag.FromErr(fmt.Errorf("A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s. %s", name, "hyperv_machine_instance", "hyperv_machine_instance", name)) + return diag.FromErr(fmt.Errorf("a resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s. %s", name, "hyperv_machine_instance", "hyperv_machine_instance", name)) } } diff --git a/internal/provider/resource_hyperv_network_switch.go b/internal/provider/resource_hyperv_network_switch.go index b197a2f3..6f5d9c5f 100644 --- a/internal/provider/resource_hyperv_network_switch.go +++ b/internal/provider/resource_hyperv_network_switch.go @@ -162,7 +162,7 @@ func resourceHyperVNetworkSwitchCreate(ctx context.Context, d *schema.ResourceDa } if existing.Exists { - return diag.FromErr(fmt.Errorf("A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s. %s", switchName, "hyperv_network_switch", "hyperv_network_switch", switchName)) + return diag.FromErr(fmt.Errorf("a resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s. %s", switchName, "hyperv_network_switch", "hyperv_network_switch", switchName)) } } @@ -185,7 +185,8 @@ func resourceHyperVNetworkSwitchCreate(ctx context.Context, d *schema.ResourceDa defaultQueueVmmqQueuePairs := int32((d.Get("default_queue_vmmq_queue_pairs")).(int)) defaultQueueVrssEnabled := (d.Get("default_queue_vrss_enabled")).(bool) - if switchType == api.VMSwitchType_Private { + switch switchType { + case api.VMSwitchType_Private: if allowManagementOS { return diag.Errorf("[ERROR][hyperv][create] Unable to set AllowManagementOS to true if switch type is private") } @@ -193,7 +194,7 @@ func resourceHyperVNetworkSwitchCreate(ctx context.Context, d *schema.ResourceDa if len(netAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][create] Unable to set NetAdapterNames when switch type is private") } - } else if switchType == api.VMSwitchType_Internal { + case api.VMSwitchType_Internal: if !allowManagementOS { return diag.Errorf("[ERROR][hyperv][create] Unable to set AllowManagementOS to false if switch type is internal") } @@ -201,27 +202,28 @@ func resourceHyperVNetworkSwitchCreate(ctx context.Context, d *schema.ResourceDa if len(netAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][create] Unable to set NetAdapterNames when switch type is internal") } - } else if switchType == api.VMSwitchType_External { + case api.VMSwitchType_External: if len(netAdapterNames) < 1 { return diag.Errorf("[ERROR][hyperv][create] Must specify NetAdapterNames if switch type is external") } } - if bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute { + switch { + case bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute: if defaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][create] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is absolute") } if defaultFlowMinimumBandwidthAbsolute < 0 { return diag.Errorf("[ERROR][hyperv][create] Bandwidth absolute must be 0 or greater") } - } else if bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)) { + case bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)): if defaultFlowMinimumBandwidthAbsolute != 0 { return diag.Errorf("[ERROR][hyperv][create] Unable to set DefaultFlowMinimumBandwidthAbsolute if bandwidth reservation mode is weight") } if defaultFlowMinimumBandwidthWeight < 1 || defaultFlowMinimumBandwidthWeight > 100 { return diag.Errorf("[ERROR][hyperv][create] Bandwidth weight must be between 1 and 100") } - } else { + default: if defaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][create] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is none") } @@ -268,7 +270,8 @@ func resourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceData return diag.FromErr(err) } - if s.SwitchType == api.VMSwitchType_Private { + switch s.SwitchType { + case api.VMSwitchType_Private: if s.AllowManagementOS { return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to true if switch type is private") } @@ -276,7 +279,7 @@ func resourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceData if len(s.NetAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is private") } - } else if s.SwitchType == api.VMSwitchType_Internal { + case api.VMSwitchType_Internal: if !s.AllowManagementOS { return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to false if switch type is internal") } @@ -284,27 +287,28 @@ func resourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceData if len(s.NetAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is internal") } - } else if s.SwitchType == api.VMSwitchType_External { + case api.VMSwitchType_External: if len(s.NetAdapterNames) < 1 { return diag.Errorf("[ERROR][hyperv][read] Must specify NetAdapterNames if switch type is external") } } - if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute { + switch { + case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute: if s.DefaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is absolute") } if s.DefaultFlowMinimumBandwidthAbsolute < 0 { return diag.Errorf("[ERROR][hyperv][read] Bandwidth absolute must be 0 or greater") } - } else if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)) { + case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)): if s.DefaultFlowMinimumBandwidthAbsolute != 0 { return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthAbsolute should be 0 if bandwidth reservation mode is weight") } if s.DefaultFlowMinimumBandwidthWeight < 1 || s.DefaultFlowMinimumBandwidthWeight > 100 { return diag.Errorf("[ERROR][hyperv][read] Bandwidth weight must be between 1 and 100") } - } else { + default: if s.DefaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is none") } @@ -389,7 +393,8 @@ func resourceHyperVNetworkSwitchUpdate(ctx context.Context, d *schema.ResourceDa defaultQueueVmmqQueuePairs := int32((d.Get("default_queue_vmmq_queue_pairs")).(int)) defaultQueueVrssEnabled := (d.Get("default_queue_vrss_enabled")).(bool) - if switchType == api.VMSwitchType_Private { + switch switchType { + case api.VMSwitchType_Private: if allowManagementOS { return diag.Errorf("[ERROR][hyperv][update] Unable to set AllowManagementOS to true if switch type is private") } @@ -397,7 +402,7 @@ func resourceHyperVNetworkSwitchUpdate(ctx context.Context, d *schema.ResourceDa if len(netAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][update] Unable to set NetAdapterNames when switch type is private") } - } else if switchType == api.VMSwitchType_Internal { + case api.VMSwitchType_Internal: if !allowManagementOS { return diag.Errorf("[ERROR][hyperv][update] Unable to set AllowManagementOS to false if switch type is internal") } @@ -405,27 +410,28 @@ func resourceHyperVNetworkSwitchUpdate(ctx context.Context, d *schema.ResourceDa if len(netAdapterNames) > 0 { return diag.Errorf("[ERROR][hyperv][update] Unable to set NetAdapterNames when switch type is internal") } - } else if switchType == api.VMSwitchType_External { + case api.VMSwitchType_External: if len(netAdapterNames) < 1 { return diag.Errorf("[ERROR][hyperv][update] Must specify NetAdapterNames if switch type is external") } } - if bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute { + switch { + case bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute: if defaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][update] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is absolute") } if defaultFlowMinimumBandwidthAbsolute < 0 { return diag.Errorf("[ERROR][hyperv][update] Bandwidth absolute must be 0 or greater") } - } else if bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)) { + case bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)): if defaultFlowMinimumBandwidthAbsolute != 0 { return diag.Errorf("[ERROR][hyperv][update] Unable to set DefaultFlowMinimumBandwidthAbsolute if bandwidth reservation mode is weight") } if defaultFlowMinimumBandwidthWeight < 1 || defaultFlowMinimumBandwidthWeight > 100 { return diag.Errorf("[ERROR][hyperv][update] Bandwidth weight must be between 1 and 100") } - } else { + default: if defaultFlowMinimumBandwidthWeight != 0 { return diag.Errorf("[ERROR][hyperv][update] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is none") } diff --git a/internal/provider/resource_hyperv_vhd.go b/internal/provider/resource_hyperv_vhd.go index 8890d70a..fe2828c4 100644 --- a/internal/provider/resource_hyperv_vhd.go +++ b/internal/provider/resource_hyperv_vhd.go @@ -126,7 +126,7 @@ func resourceHyperVVhd() *schema.Resource { ConflictsWith: []string{ "parent_path", }, - ValidateDiagFunc: IsDivisibleBy(4096), //Technical it could also be 512 + ValidateDiagFunc: IsDivisibleBy(4096), // Technical it could also be 512 Description: "This field is mutually exclusive with the field `parent_path`. The maximum size, in bytes, of the virtual hard disk to be created. This size must be divisible by 4096 so that it fits into logical blocks.", }, "block_size": { @@ -232,7 +232,7 @@ func resourceHyperVVhdCreate(ctx context.Context, d *schema.ResourceData, meta i } if existing.Exists { - return diag.FromErr(fmt.Errorf("A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s. %s", path, "hyperv_vhd", "hyperv_vhd", path)) + return diag.FromErr(fmt.Errorf("a resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s. %s", path, "hyperv_vhd", "hyperv_vhd", path)) } } diff --git a/powershell/provisioner.go b/powershell/provisioner.go index 90af9bc9..42cc97d1 100644 --- a/powershell/provisioner.go +++ b/powershell/provisioner.go @@ -29,7 +29,7 @@ func winPath(path string) string { path = fmt.Sprintf("'%s'", strings.Trim(path, "'\"")) } - return strings.Replace(path, "/", "\\", -1) + return strings.ReplaceAll(path, "/", "\\") } func doCopy(client *winrm.Client, maxChunks int, in io.Reader, toPath string) (remoteAbsolutePath string, err error) { @@ -375,6 +375,9 @@ func shellExecute(shell *winrm.Shell, command string, arguments ...string) (int, func uploadScript(client *winrm.Client, fileName string, command string) (remoteAbsolutePath string, err error) { tmpFile, err := ioutil.TempFile(os.TempDir(), fileName) + if err != nil { + return "", fmt.Errorf("error creating temp file: %s", err) + } writer := bufio.NewWriter(tmpFile) if _, err := writer.WriteString(command); err != nil { return "", fmt.Errorf("error preparing shell script: %s", err) diff --git a/powershell/template.go b/powershell/template.go index 273e8401..c982aa18 100644 --- a/powershell/template.go +++ b/powershell/template.go @@ -11,10 +11,10 @@ type executePowershellFromCommandLineTemplateOptions struct { var executePowershellFromCommandLineTemplate = template.Must(template.New("ExecuteCommandFromCommandLine").Funcs(template.FuncMap{ "escapeDoubleQuotes": func(textToEscape string) string { - textToEscape = strings.Replace(textToEscape, "\n", "", -1) - textToEscape = strings.Replace(textToEscape, "\r", "", -1) - textToEscape = strings.Replace(textToEscape, "\t", "", -1) - textToEscape = strings.Replace(textToEscape, `"`, `\"`, -1) + textToEscape = strings.ReplaceAll(textToEscape, "\n", "") + textToEscape = strings.ReplaceAll(textToEscape, "\r", "") + textToEscape = strings.ReplaceAll(textToEscape, "\t", "") + textToEscape = strings.ReplaceAll(textToEscape, `"`, `\"`) return textToEscape }, }).Parse(`powershell -NoProfile -ExecutionPolicy Bypass "{{escapeDoubleQuotes .Powershell}}"`)) @@ -38,7 +38,7 @@ type elevatedCommandTemplateOptions struct { var elevatedCommandTemplate = template.Must(template.New("ElevatedCommand").Funcs(template.FuncMap{ "escapeSingleQuotes": func(textToEscape string) string { - return strings.Replace(textToEscape, `'`, `''`, -1) + return strings.ReplaceAll(textToEscape, `'`, `''`) }, }).Parse(` function GetTempFile($fileName) {