Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename post files to upload
  • Loading branch information
kylecarbs committed Feb 25, 2022
commit 773e38f02582be1fbf46a839fcb82dcae6b591fb
4 changes: 2 additions & 2 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ func New(options *Options) (http.Handler, func()) {
})
})

r.Route("/files", func(r chi.Router) {
r.Route("/upload", func(r chi.Router) {
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
r.Post("/", api.postFiles)
r.Post("/", api.postUpload)
})

r.Route("/projectimport/{organization}", func(r chi.Router) {
Expand Down
5 changes: 0 additions & 5 deletions coderd/downloads.go

This file was deleted.

2 changes: 1 addition & 1 deletion coderd/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type UploadFileResponse struct {
Hash string `json:"hash"`
}

func (api *api) postFiles(rw http.ResponseWriter, r *http.Request) {
func (api *api) postUpload(rw http.ResponseWriter, r *http.Request) {
apiKey := httpmw.APIKey(r)
contentType := r.Header.Get("Content-Type")

Expand Down
2 changes: 1 addition & 1 deletion coderd/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/coder/coder/codersdk"
)

func TestPostFiles(t *testing.T) {
func TestPostUpload(t *testing.T) {
t.Parallel()
t.Run("BadContentType", func(t *testing.T) {
t.Parallel()
Expand Down
2 changes: 1 addition & 1 deletion codersdk/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
)

func (c *Client) UploadFile(ctx context.Context, contentType string, content []byte) (coderd.UploadFileResponse, error) {
res, err := c.request(ctx, http.MethodPost, "/api/v2/files", content, func(r *http.Request) {
res, err := c.request(ctx, http.MethodPost, "/api/v2/upload", content, func(r *http.Request) {
r.Header.Set("Content-Type", contentType)
})
if err != nil {
Expand Down
49 changes: 42 additions & 7 deletions provisioner/terraform/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,74 @@ package provider
import (
"context"
"fmt"
"net/url"
"os"
"strings"

"github.com/coder/coder/provisionersdk"
"github.com/google/uuid"
"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"
)

// New returns a new schema provider for Terraform.
type config struct {
URL *url.URL
}

// New returns a new Terraform provider.
func New() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"workspace_history_id": {
"url": {
Type: schema.TypeString,
Optional: true,
// The "CODER_URL" environment variable is used by default
// as the Access URL when generating scripts.
DefaultFunc: func() (interface{}, error) {
return os.Getenv("CODER_URL"), nil
},
ValidateFunc: func(i interface{}, s string) ([]string, []error) {
_, err := url.Parse(s)
if err != nil {
return nil, []error{err}
}
return nil, nil
},
},
},
ConfigureContextFunc: func(c context.Context, rd *schema.ResourceData) (interface{}, diag.Diagnostics) {
rawURL := rd.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))
if err != nil {
return nil, diag.FromErr(err)
}
return config{
URL: parsed,
}, nil
},
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)

fmt.Printf("Got OS: %s_%s\n", os, arch)

err := rd.Set("value", "SOME SCRIPT")
script, err := provisionersdk.AgentScript(os, arch, config.URL)
if err != nil {
return diag.FromErr(err)
}
err = rd.Set("value", script)
if err != nil {
return diag.FromErr(err)
}
rd.SetId("something")
rd.SetId(strings.Join([]string{os, arch}, "_"))
return nil
},
Schema: map[string]*schema.Schema{
Expand Down
20 changes: 14 additions & 6 deletions provisioner/terraform/provider/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package provider_test

import (
"encoding/json"
"fmt"
"testing"

Expand All @@ -27,8 +26,12 @@ func TestSomething(t *testing.T) {
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `data "coder_agent_script" "new" {
arch = "x64"
Config: `
provider "coder" {
url = "https://example.com"
}
data "coder_agent_script" "new" {
arch = "amd64"
os = "linux"
}`,
Check: func(s *terraform.State) error {
Expand All @@ -46,7 +49,12 @@ func TestAnother(t *testing.T) {
},
IsUnitTest: true,
Steps: []resource.TestStep{{
Config: `resource "coder_agent" "new" {
Config: `
provider "coder" {
url = "https://example.com"
}

resource "coder_agent" "new" {
auth {
type = "gcp"
}
Expand All @@ -59,8 +67,8 @@ func TestAnother(t *testing.T) {
// for _, mod := range s.Modules {
// fmt.Printf("check state: %+v\n", mod.Resources)
// }
data, _ := json.MarshalIndent(s, "", "\t")
fmt.Printf("Data: %s\n", data)
// data, _ := json.MarshalIndent(s, "", "\t")
// fmt.Printf("Data: %s\n", data)

return nil
},
Expand Down
20 changes: 7 additions & 13 deletions provisionersdk/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,29 @@ exec $BINARY_LOCATION agent
}
)

// AgentBinaryName returns the binary name for an operating system and agent.
func AgentBinaryName(operatingSystem, architecture string) (string, error) {

}
// CODER_URL, operating system, and architecture can return the URL of the Coder binary.

// AgentScript returns an installation script for the specified operating system
// and architecture.
//
// baseURL is
func AgentScript(operatingSystem, architecture string, baseURL *url.URL) (string, error) {
architectures, ok := agentScript[operatingSystem]
if !ok {
func AgentScript(operatingSystem, architecture string, binaryDownloadURL *url.URL) (string, error) {
architectures, exists := agentScript[operatingSystem]
if !exists {
list := []string{}
for key := range agentScript {
list = append(list, key)
}
return "", xerrors.Errorf("operating system %q not supported. must be in: %v", operatingSystem, list)
}
script, ok := architectures[architecture]
if !ok {
script, exists := architectures[architecture]
if !exists {
list := []string{}
for key := range architectures {
list = append(list, key)
}
return "", xerrors.Errorf("architecture %q not supported for %q. must be in: %v", architecture, operatingSystem, list)
}
if !strings.HasPrefix(baseURL.Path, "/api/v2/downloads") {

}

return strings.ReplaceAll(script, "${DOWNLOAD_URL}", baseURL.String()), nil
return strings.ReplaceAll(script, "${DOWNLOAD_URL}", binaryDownloadURL.String()), nil
}