Skip to content
Prev Previous commit
Next Next commit
Fix linting errors
  • Loading branch information
kylecarbs committed Feb 26, 2022
commit f8cab499cf28a8384a5004be96738b30702d271f
3 changes: 2 additions & 1 deletion cmd/terraform-provider-coder/main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/coder/coder/provisioner/terraform/provider"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"

"github.com/coder/coder/provisioner/terraform/provider"
)

func main() {
Expand Down
41 changes: 27 additions & 14 deletions provisioner/terraform/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/url"
"os"
"reflect"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -39,12 +40,15 @@ func New() *schema.Provider {
},
},
},
ConfigureContextFunc: func(c context.Context, rd *schema.ResourceData) (interface{}, diag.Diagnostics) {
rawURL := rd.Get("url").(string)
ConfigureContextFunc: func(c context.Context, resourceData *schema.ResourceData) (interface{}, diag.Diagnostics) {
rawURL, ok := resourceData.Get("url").(string)
if !ok {
return nil, diag.Errorf("unexpected type %q for url", reflect.TypeOf(resourceData.Get("url")).String())
}
if rawURL == "" {
return nil, diag.Errorf("CODER_URL must not be empty; got %q", rawURL)
}
parsed, err := url.Parse(rd.Get("url").(string))
parsed, err := url.Parse(resourceData.Get("url").(string))
if err != nil {
return nil, diag.FromErr(err)
}
Expand All @@ -55,22 +59,28 @@ func New() *schema.Provider {
DataSourcesMap: map[string]*schema.Resource{
"coder_agent_script": {
Description: "TODO",
ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
config := i.(config)
osRaw := rd.Get("os")
os := osRaw.(string)
archRaw := rd.Get("arch")
arch := archRaw.(string)

script, err := provisionersdk.AgentScript(config.URL, os, arch)
ReadContext: func(c context.Context, resourceData *schema.ResourceData, i interface{}) diag.Diagnostics {
config, valid := i.(config)
if !valid {
return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String())
}
operatingSystem, valid := resourceData.Get("os").(string)
if !valid {
return diag.Errorf("os was unexpected type %q", reflect.TypeOf(resourceData.Get("os")))
}
arch, valid := resourceData.Get("arch").(string)
if !valid {
return diag.Errorf("arch was unexpected type %q", reflect.TypeOf(resourceData.Get("arch")))
}
script, err := provisionersdk.AgentScript(config.URL, operatingSystem, arch)
if err != nil {
return diag.FromErr(err)
}
err = rd.Set("value", script)
err = resourceData.Set("value", script)
if err != nil {
return diag.FromErr(err)
}
rd.SetId(strings.Join([]string{os, arch}, "_"))
resourceData.SetId(strings.Join([]string{operatingSystem, arch}, "_"))
return nil
},
Schema: map[string]*schema.Schema{
Expand All @@ -96,7 +106,10 @@ func New() *schema.Provider {
CreateContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
// This should be a real authentication token!
rd.SetId(uuid.NewString())
rd.Set("token", uuid.NewString())
err := rd.Set("token", uuid.NewString())
if err != nil {
return diag.FromErr(err)
}
return nil
},
ReadContext: func(c context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down
12 changes: 7 additions & 5 deletions provisioner/terraform/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestAgentScript(t *testing.T) {
func TestAgent(t *testing.T) {
t.Parallel()
t.Run("Empty", func(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
Expand All @@ -76,6 +77,7 @@ func TestAgent(t *testing.T) {
})

t.Run("Filled", func(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
Providers: map[string]*schema.Provider{
"coder": provider.New(),
Expand All @@ -101,17 +103,17 @@ func TestAgent(t *testing.T) {
require.Len(t, state.Modules[0].Resources, 1)
resource := state.Modules[0].Resources["coder_agent.new"]
require.NotNil(t, resource)
for _, k := range []string{
for _, key := range []string{
"token",
"auth.0.type",
"auth.0.instance_id",
"env.hi",
"startup_script",
} {
v := resource.Primary.Attributes[k]
t.Log(fmt.Sprintf("%q = %q", k, v))
require.NotNil(t, v)
require.Greater(t, len(v), 0)
value := resource.Primary.Attributes[key]
t.Log(fmt.Sprintf("%q = %q", key, value))
require.NotNil(t, value)
require.Greater(t, len(value), 0)
}
return nil
},
Expand Down
28 changes: 22 additions & 6 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"strings"

"cdr.dev/slog"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/mitchellh/mapstructure"
"golang.org/x/xerrors"

"cdr.dev/slog"

"github.com/coder/coder/provisionersdk/proto"
)

Expand Down Expand Up @@ -165,24 +167,38 @@ func (t *terraform) runTerraformPlan(ctx context.Context, terraform *tfexec.Terr
agent := &proto.Agent{
Auth: &proto.Agent_Token{},
}
if env, has := resource.Expressions["env"]; has {
agent.Env = env.ConstantValue.(map[string]string)
if envRaw, has := resource.Expressions["env"]; has {
env, ok := envRaw.ConstantValue.(map[string]string)
if !ok {
return xerrors.Errorf("unexpected type %q for env map", reflect.TypeOf(envRaw.ConstantValue).String())
}
agent.Env = env
}
if startupScript, has := resource.Expressions["startup_script"]; has {
agent.StartupScript = startupScript.ConstantValue.(string)
if startupScriptRaw, has := resource.Expressions["startup_script"]; has {
startupScript, ok := startupScriptRaw.ConstantValue.(string)
if !ok {
return xerrors.Errorf("unexpected type %q for startup script", reflect.TypeOf(startupScriptRaw.ConstantValue).String())
}
agent.StartupScript = startupScript
}
if auth, has := resource.Expressions["auth"]; has {
if len(auth.ExpressionData.NestedBlocks) > 0 {
block := auth.ExpressionData.NestedBlocks[0]
authType, has := block["type"]
if has {
switch authType.ConstantValue.(string) {
authTypeValue, valid := authType.ConstantValue.(string)
if !valid {
return xerrors.Errorf("unexpected type %q for auth type", reflect.TypeOf(authType.ConstantValue))
}
switch authTypeValue {
case "google-instance-identity":
agent.Auth = &proto.Agent_GoogleInstanceIdentity{
GoogleInstanceIdentity: &proto.GoogleInstanceIdentityAuth{
InstanceId: block["instance_id"].ConstantValue.(string),
},
}
default:
return xerrors.Errorf("unknown auth type: %q", authTypeValue)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
"runtime"
"testing"

"github.com/stretchr/testify/require"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"
"github.com/stretchr/testify/require"

"github.com/coder/coder/provisioner/terraform"
"github.com/coder/coder/provisionersdk"
Expand All @@ -30,6 +31,7 @@ func TestProvision(t *testing.T) {
providerDest := filepath.Join(homeDir, ".terraform.d", "plugins", "coder.com", "internal", "coder", "0.0.1", fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH))
err = os.MkdirAll(providerDest, 0700)
require.NoError(t, err)
//nolint:dogsled
_, filename, _, _ := runtime.Caller(0)
providerSrc := filepath.Join(filepath.Dir(filename), "..", "..", "cmd", "terraform-provider-coder")
output, err := exec.Command("go", "build", "-o", providerDest, providerSrc).CombinedOutput()
Expand Down
3 changes: 2 additions & 1 deletion provisionersdk/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import (
"strings"
"testing"

"github.com/coder/coder/provisionersdk"
"github.com/go-chi/render"
"github.com/stretchr/testify/require"

"github.com/coder/coder/provisionersdk"
)

func TestAgentScript(t *testing.T) {
Expand Down