Skip to content

Commit

Permalink
Update provider to work with Nebraska 2.5+ (#14)
Browse files Browse the repository at this point in the history
* Update provider to work with Nebraska 2.5+

API has been updated for a number of objects, so we need to update the
client.

There is also a new package available that has the types that get
returned by API, we can use that instead of redefining the types
ourselves.

* Update test Terraform and Nebraska versions

* Github actions doesn't like some bash options

```
4: set: Illegal option -o pipefail
```
  • Loading branch information
george-angel authored Mar 21, 2022
1 parent f7899ed commit e88a722
Show file tree
Hide file tree
Showing 16 changed files with 1,484 additions and 208 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ jobs:
fail-fast: false
matrix:
nebraska:
- '2.3.5'
- '2.4.1'
- '2.6.1'
# list whatever Terraform versions here you would like to support
terraform:
- '1.0.4'
- '1.1.7'
steps:

- name: Set up Go
Expand Down
38 changes: 34 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/hashicorp/terraform-plugin-docs v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.12.0
github.com/kinvolk/nebraska/backend v0.0.0-20220318103735-d7824a66ccc3
gotest.tools v2.2.0+incompatible
)

Expand All @@ -16,11 +17,19 @@ require (
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.9.1 // indirect
github.com/doug-martin/goqu/v9 v9.18.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/getkin/kin-openapi v0.92.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-gorp/gorp/v3 v3.0.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand All @@ -42,6 +51,21 @@ require (
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.10.0 // indirect
github.com/jackc/pgx/v4 v4.15.0 // indirect
github.com/jhump/protoreflect v1.6.1 // indirect
github.com/jmoiron/sqlx v1.3.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/labstack/echo/v4 v4.7.2 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/cli v1.1.2 // indirect
Expand All @@ -53,17 +77,23 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/posener/complete v1.1.1 // indirect
github.com/rs/zerolog v1.26.1 // indirect
github.com/rubenv/sql-migrate v1.1.1 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220314164441-57ef72a4c106 // indirect
google.golang.org/genproto v0.0.0-20220317150908-0efb43f6373e // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/guregu/null.v4 v4.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
1,325 changes: 1,315 additions & 10 deletions go.sum

Large diffs are not rendered by default.

13 changes: 9 additions & 4 deletions internal/provider/data_source_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/kinvolk/nebraska/backend/pkg/api"
)

func dataSourceChannel() *schema.Resource {
Expand Down Expand Up @@ -61,16 +63,19 @@ func dataSourceChannelRead(ctx context.Context, d *schema.ResourceData, meta int
return diag.FromErr(err)
}
d.Set("application_id", appID)
channels, err := c.ListChannels(appID)
channelPage, err := c.ListChannels(appID)
if err != nil {
return diag.FromErr(err)
}
if channelPage.Count != channelPage.TotalCount {
return diag.FromErr(fmt.Errorf("GET channels returned %d/%d channels. We don't paginate.", channelPage.Count, channelPage.TotalCount))
}
name := d.Get("name").(string)
arch := d.Get("arch").(string)

for _, c := range channels {
if c.Name == name && c.Arch.String() == arch {
d.SetId(c.ID)
for _, c := range channelPage.Channels {
if c.Name == name && api.Arch(c.Arch).String() == arch {
d.SetId(c.Id)
d.Set("color", c.Color)
d.Set("created_ts", c.CreatedTs.String())
d.Set("package_id", c.PackageID)
Expand Down
10 changes: 7 additions & 3 deletions internal/provider/data_source_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -98,14 +99,17 @@ func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.FromErr(err)
}
d.Set("application_id", appID)
groups, err := c.ListGroups(appID)
groupPage, err := c.ListGroups(appID)
if err != nil {
return diag.FromErr(err)
}
if groupPage.Count != groupPage.TotalCount {
return diag.FromErr(fmt.Errorf("GET groups returned %d/%d groups. We don't paginate.", groupPage.Count, groupPage.TotalCount))
}
name := d.Get("name").(string)
for _, g := range groups {
for _, g := range groupPage.Groups {
if g.Name == name {
d.SetId(g.ID)
d.SetId(g.Id)
d.Set("description", g.Description)
d.Set("created_ts", g.CreatedTs.String())
d.Set("rollout_in_progress", g.RolloutInProgress)
Expand Down
18 changes: 12 additions & 6 deletions internal/provider/data_source_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/kinvolk/nebraska/backend/pkg/api"
"github.com/utilitywarehouse/terraform-provider-nebraska/nebraska"
)

func dataSourcePackage() *schema.Resource {
Expand Down Expand Up @@ -140,18 +143,21 @@ func dataSourcePackageRead(ctx context.Context, d *schema.ResourceData, meta int
return diag.FromErr(err)
}
d.Set("application_id", appID)
packages, err := c.ListPackages(appID)
packagePage, err := c.ListPackages(appID)
if err != nil {
return diag.FromErr(err)
}
if packagePage.Count != packagePage.TotalCount {
return diag.FromErr(fmt.Errorf("GET packages returned %d/%d packages. We don't paginate.", packagePage.Count, packagePage.TotalCount))
}
version := d.Get("version").(string)
arch := d.Get("arch").(string)

for _, p := range packages {
if p.Version == version && p.Arch.String() == arch {
d.SetId(p.ID)
d.Set("type", p.Type.String())
d.Set("url", p.URL)
for _, p := range packagePage.Packages {
if p.Version == version && api.Arch(p.Arch).String() == arch {
d.SetId(p.Id)
d.Set("type", nebraska.PackageType(p.Type).String())
d.Set("url", p.Url)
d.Set("filename", p.Filename)
d.Set("description", p.Description)
d.Set("size", p.Size)
Expand Down
15 changes: 9 additions & 6 deletions internal/provider/resource_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/kinvolk/nebraska/backend/pkg/api"
"github.com/kinvolk/nebraska/backend/pkg/codegen"
"github.com/utilitywarehouse/terraform-provider-nebraska/nebraska"
)

Expand Down Expand Up @@ -68,7 +70,7 @@ func resourceChannelCreate(ctx context.Context, d *schema.ResourceData, meta int
}
d.Set("application_id", appID)

arch, err := nebraska.ArchFromString(d.Get("arch").(string))
arch, err := api.ArchFromString(d.Get("arch").(string))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -77,15 +79,15 @@ func resourceChannelCreate(ctx context.Context, d *schema.ResourceData, meta int
Name: d.Get("name").(string),
Color: d.Get("color").(string),
PackageID: d.Get("package_id").(string),
Arch: arch,
Arch: codegen.Arch(arch),
}

channel, err := c.AddChannel(appID, input)
if err != nil {
return diag.FromErr(err)
}

d.SetId(channel.ID)
d.SetId(channel.Id)

return resourceChannelRead(ctx, d, meta)
}
Expand Down Expand Up @@ -140,9 +142,10 @@ func resourceChannelUpdate(ctx context.Context, d *schema.ResourceData, meta int
}

input := &nebraska.UpdateChannelInput{
Name: d.Get("name").(string),
Color: d.Get("color").(string),
PackageID: d.Get("package_id").(string),
Name: d.Get("name").(string),
Color: d.Get("color").(string),
PackageID: d.Get("package_id").(string),
ApplicationID: appID,
}

if _, err := c.UpdateChannel(appID, d.Id(), input); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func resourceGroupCreate(ctx context.Context, d *schema.ResourceData, meta inter
return diag.FromErr(err)
}

d.SetId(group.ID)
d.SetId(group.Id)

return resourceGroupRead(ctx, d, meta)
}
Expand Down
32 changes: 21 additions & 11 deletions internal/provider/resource_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/kinvolk/nebraska/backend/pkg/api"
"github.com/kinvolk/nebraska/backend/pkg/codegen"
"github.com/utilitywarehouse/terraform-provider-nebraska/nebraska"
)

Expand Down Expand Up @@ -36,7 +38,7 @@ func resourcePackage() *schema.Resource {
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(nebraska.ValidArchs, false),
Default: nebraska.ArchAll.String(),
Default: api.ArchAll.String(),
Description: "Package arch.",
},
"type": {
Expand Down Expand Up @@ -155,7 +157,7 @@ func resourcePackageCreate(ctx context.Context, d *schema.ResourceData, meta int
}
d.Set("application_id", appID)

arch, err := nebraska.ArchFromString(d.Get("arch").(string))
arch, err := api.ArchFromString(d.Get("arch").(string))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -174,18 +176,22 @@ func resourcePackageCreate(ctx context.Context, d *schema.ResourceData, meta int
Size: d.Get("size").(string),
Hash: d.Get("hash").(string),
ChannelsBlacklist: expandChannelBlacklist(d.Get("channels_blacklist").([]interface{})),
Arch: arch,
Arch: codegen.Arch(arch),
ApplicationID: nebraska.FlatcarApplicationID,
FlatcarAction: &nebraska.FlatcarActionInput{
Sha256: expandFlatcarActionSha256(d.Get("flatcar_action").([]interface{})),
},
}

if len(input.ChannelsBlacklist) == 0 {
input.ChannelsBlacklist = make([]string, 0)
}
pkg, err := c.AddPackage(appID, input)
if err != nil {
return diag.FromErr(err)
}

d.SetId(pkg.ID)
d.SetId(pkg.Id)

return resourcePackageRead(ctx, d, meta)
}
Expand All @@ -212,15 +218,15 @@ func resourcePackageRead(ctx context.Context, d *schema.ResourceData, meta inter
return nil
}

if err := d.Set("type", pkg.Type.String()); err != nil {
if err := d.Set("type", nebraska.PackageType(pkg.Type).String()); err != nil {
return diag.FromErr(err)
}

if err := d.Set("version", pkg.Version); err != nil {
return diag.FromErr(err)
}

if err := d.Set("url", pkg.URL); err != nil {
if err := d.Set("url", pkg.Url); err != nil {
return diag.FromErr(err)
}

Expand Down Expand Up @@ -248,7 +254,7 @@ func resourcePackageRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.FromErr(err)
}

if err := d.Set("arch", pkg.Arch.String()); err != nil {
if err := d.Set("arch", api.Arch(pkg.Arch).String()); err != nil {
return diag.FromErr(err)
}

Expand All @@ -264,7 +270,7 @@ func resourcePackageUpdate(ctx context.Context, d *schema.ResourceData, meta int
}
d.Set("application_id", appID)

arch, err := nebraska.ArchFromString(d.Get("arch").(string))
arch, err := api.ArchFromString(d.Get("arch").(string))
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -283,12 +289,16 @@ func resourcePackageUpdate(ctx context.Context, d *schema.ResourceData, meta int
Size: d.Get("size").(string),
Hash: d.Get("hash").(string),
ChannelsBlacklist: expandChannelBlacklist(d.Get("channels_blacklist").([]interface{})),
Arch: arch,
Arch: codegen.Arch(arch),
ApplicationID: nebraska.FlatcarApplicationID,
FlatcarAction: &nebraska.FlatcarActionInput{
Sha256: expandFlatcarActionSha256(d.Get("flatcar_action").([]interface{})),
},
}

if len(input.ChannelsBlacklist) == 0 {
input.ChannelsBlacklist = make([]string, 0)
}
if _, err := c.UpdatePackage(appID, d.Id(), input); err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -335,14 +345,14 @@ func expandFlatcarActionSha256(l []interface{}) string {
return ""
}

func flattenFlatcarAction(action *nebraska.FlatcarAction) []map[string]interface{} {
func flattenFlatcarAction(action *codegen.FlatcarAction) []map[string]interface{} {
if action == nil {
return []map[string]interface{}{}
}

return []map[string]interface{}{
{
"id": action.ID,
"id": action.Id,
"event": action.Event,
"chromeos_version": action.ChromeOSVersion,
"sha256": action.Sha256,
Expand Down
Loading

0 comments on commit e88a722

Please sign in to comment.