Skip to content

Commit 0766787

Browse files
authored
Add support for importing the PKI CRL config (hashicorp#1710)
- Adapt tests to support older vault versions - Make some API data and schema set operations simpler - Fix broken build
1 parent d0d7987 commit 0766787

13 files changed

+283
-180
lines changed

.github/workflows/build.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ name: Build
33
on: push
44

55
jobs:
6+
gh-api-quota-check:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: get GH rate-limit config
10+
run: |
11+
curl -H "Accept: application/vnd.github+json" \
12+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
13+
-H "X-GitHub-Api-Version: 2022-11-28" \
14+
https://api.github.com/rate_limit
615
go-version:
716
runs-on: ubuntu-latest
817
outputs:
918
version: ${{ steps.go-version.outputs.version }}
1019
steps:
1120
- uses: actions/checkout@v3
1221
- id: go-version
13-
run: echo "::set-output name=version::$(cat ./.go-version)"
22+
run: echo "version=$(cat .go-version)" >> $GITHUB_OUTPUT
1423
build:
1524
needs: [go-version]
1625
runs-on: ubuntu-latest
@@ -23,6 +32,8 @@ jobs:
2332
make build
2433
- name: Run unit tests
2534
# here to short-circuit the acceptance tests, in the case of a failure.
35+
env:
36+
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
2637
run: |
2738
make test
2839
acceptance:

.github/workflows/issue-opened.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ jobs:
88
issue_triage:
99
runs-on: ubuntu-latest
1010
steps:
11-
- uses: actions/checkout@v2
11+
- uses: actions/checkout@v3
1212
- uses: github/[email protected]
1313
with:
1414
repo-token: "${{ secrets.GITHUB_TOKEN }}"
1515
configuration-path: .github/labeler-issue-triage.yml
16+
# TODO: update to use action/labeler https://github.com/actions/labeler

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ terraform-provider-vault
3737

3838
# Scratch directory for miscellaneous files/examples/etc.
3939
scratch
40+
41+
# others
42+
.swp

.go-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.17.10
1+
1.19.4

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Requirements
2323
------------
2424

2525
- [Terraform](https://www.terraform.io/downloads.html) 0.12.x and above, we recommend using the latest stable release whenever possible.
26-
- [Go](https://golang.org/doc/install) 1.17 (to build the provider plugin)
26+
- [Go](https://golang.org/doc/install) 1.19 (to build the provider plugin)
2727

2828
Building The Provider
2929
---------------------
@@ -45,7 +45,7 @@ $ make build
4545
Developing the Provider
4646
---------------------------
4747

48-
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.16+ 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`.
48+
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (version 1.19+ 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`.
4949

5050
To compile the provider, run `make build`. This will build the provider and put the provider binary in the `$GOPATH/bin` directory.
5151

generated/resources/transform/role/name.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func NameResource() *schema.Resource {
4848
Exists: resourceNameExists,
4949
Delete: deleteNameResource,
5050
Importer: &schema.ResourceImporter{
51-
State: schema.ImportStatePassthrough,
51+
StateContext: schema.ImportStatePassthroughContext,
5252
},
5353
Schema: fields,
5454
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
github.com/containerd/containerd v1.6.6 // indirect
1818
github.com/coreos/go-oidc/v3 v3.4.0 // indirect
1919
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f
20+
github.com/davecgh/go-spew v1.1.1
2021
github.com/denisenkom/go-mssqldb v0.12.0
2122
github.com/docker/distribution v2.8.1+incompatible // indirect
2223
github.com/go-sql-driver/mysql v1.6.0

testutil/testutil.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"reflect"
1212
"strconv"
1313
"strings"
14+
"sync"
1415
"testing"
1516

1617
"github.com/coreos/pkg/multierror"
@@ -238,17 +239,16 @@ type GHOrgResponse struct {
238239
}
239240

240241
// cache GH API responses to avoid triggering the GH request rate limiter
241-
var ghOrgResponseCache = map[string]*GHOrgResponse{}
242+
var ghOrgResponseCache = sync.Map{}
242243

243244
// GetGHOrgResponse returns the GH org's meta configuration.
244245
func GetGHOrgResponse(t *testing.T, org string) *GHOrgResponse {
245246
t.Helper()
246247

247-
if v, ok := ghOrgResponseCache[org]; ok {
248-
return v
249-
}
250-
251248
client := newGHRESTClient()
249+
if v, ok := ghOrgResponseCache.Load(org); ok {
250+
return v.(*GHOrgResponse)
251+
}
252252

253253
result := &GHOrgResponse{}
254254
if err := client.get(fmt.Sprintf("orgs/%s", org), result); err != nil {
@@ -259,7 +259,7 @@ func GetGHOrgResponse(t *testing.T, org string) *GHOrgResponse {
259259
t.Fatalf("expected org %q from GH API response, actual %q", org, result.Login)
260260
}
261261

262-
ghOrgResponseCache[org] = result
262+
ghOrgResponseCache.Store(org, result)
263263

264264
return result
265265
}
@@ -288,9 +288,11 @@ func (c *ghRESTClient) do(method, path string, v interface{}) error {
288288
}
289289

290290
req.Header.Set("Accept", "application/vnd.github.v3+json")
291+
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
291292
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
292293
req.Header.Set("Authorization", "Bearer "+token)
293294
}
295+
294296
resp, err := c.client.Do(req)
295297
if err != nil {
296298
return err

testutil/testutil_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package testutil
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -275,3 +276,44 @@ func Test_assertVaultState(t *testing.T) {
275276
})
276277
}
277278
}
279+
280+
func TestGetGHOrgResponse(t *testing.T) {
281+
tests := []struct {
282+
name string
283+
org string
284+
want *GHOrgResponse
285+
}{
286+
{
287+
name: "hashicorp",
288+
org: "hashicorp",
289+
want: &GHOrgResponse{
290+
Login: "hashicorp",
291+
ID: 761456,
292+
},
293+
},
294+
{
295+
name: "github",
296+
org: "github",
297+
want: &GHOrgResponse{
298+
Login: "github",
299+
ID: 9919,
300+
},
301+
},
302+
}
303+
for _, tt := range tests {
304+
t.Run(tt.name, func(t *testing.T) {
305+
if got := GetGHOrgResponse(t, tt.org); !reflect.DeepEqual(got, tt.want) {
306+
t.Errorf("GetGHOrgResponse() = %v, want %v", got, tt.want)
307+
}
308+
v, ok := ghOrgResponseCache.Load(tt.org)
309+
if !ok {
310+
t.Fatalf("GetGHOrgResponse() result not cached for %s", tt.org)
311+
}
312+
313+
got := v.(*GHOrgResponse)
314+
if !reflect.DeepEqual(got, tt.want) {
315+
t.Errorf("GetGHOrgResponse() = %v, want %v", got, tt.want)
316+
}
317+
})
318+
}
319+
}

util/util.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -360,16 +360,41 @@ func GetAPIRequestDataWithSlice(d *schema.ResourceData, fields []string) map[str
360360
return data
361361
}
362362

363+
// GetAPIRequestDataWithSliceOk to pass to Vault from schema.ResourceData.
364+
// Only field values that are set in schema.ResourceData will be returned
365+
func GetAPIRequestDataWithSliceOk(d *schema.ResourceData, fields []string) map[string]interface{} {
366+
data := make(map[string]interface{})
367+
for _, k := range fields {
368+
if v, ok := getAPIRequestValueOk(d, k); ok {
369+
data[k] = v
370+
}
371+
}
372+
373+
return data
374+
}
375+
363376
func getAPIRequestValue(d *schema.ResourceData, k string) interface{} {
364-
sv := d.Get(k)
365-
switch v := sv.(type) {
377+
return getAPIValue(d.Get(k))
378+
}
379+
380+
func getAPIValue(i interface{}) interface{} {
381+
switch s := i.(type) {
366382
case *schema.Set:
367-
return v.List()
383+
return s.List()
368384
default:
369-
return sv
385+
return s
370386
}
371387
}
372388

389+
func getAPIRequestValueOk(d *schema.ResourceData, k string) (interface{}, bool) {
390+
sv, ok := d.GetOk(k)
391+
if !ok {
392+
return nil, ok
393+
}
394+
395+
return getAPIValue(sv), ok
396+
}
397+
373398
func Remount(d *schema.ResourceData, client *api.Client, mountField string, isAuthMount bool) (string, error) {
374399
ret := d.Get(mountField).(string)
375400

0 commit comments

Comments
 (0)