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
Fix plan resource parsing
  • Loading branch information
kylecarbs committed Feb 28, 2022
commit 3f99334ed2acb73634940a49614d4bea51e27689
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"stretchr",
"tcpip",
"tfexec",
"tfjson",
"tfstate",
"unconvert",
"webrtc",
Expand Down
15 changes: 8 additions & 7 deletions cli/projectcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,6 @@ func projectCreate() *cobra.Command {
if err != nil {
return err
}
project, err := client.CreateProject(cmd.Context(), organization.Name, coderd.CreateProjectRequest{
Name: name,
VersionImportJobID: job.ID,
})
if err != nil {
return err
}

_, err = prompt(cmd, &promptui.Prompt{
Label: "Create project?",
Expand All @@ -91,6 +84,14 @@ func projectCreate() *cobra.Command {
return err
}

project, err := client.CreateProject(cmd.Context(), organization.Name, coderd.CreateProjectRequest{
Name: name,
VersionImportJobID: job.ID,
})
if err != nil {
return err
}

_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s The %s project has been created!\n", caret, color.HiCyanString(project.Name))
_, err = prompt(cmd, &promptui.Prompt{
Label: "Create a new workspace?",
Expand Down
3 changes: 3 additions & 0 deletions cli/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func displayProjectImportInfo(cmd *cobra.Command, parameterSchemas []coderd.Para
if resource.Transition == database.WorkspaceTransitionStop {
transition = color.HiRedString("stop")
}
if resource.AgentID != nil {
transition += " with agent"
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), " %s %s on %s\n\n", color.HiCyanString(resource.Type), color.HiCyanString(resource.Name), transition)
}
return nil
Expand Down
6 changes: 5 additions & 1 deletion coderd/projectimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ func (api *api) projectImportJobResourcesByID(rw http.ResponseWriter, r *http.Re
if resources == nil {
resources = []database.ProvisionerJobResource{}
}
apiResources := make([]ProvisionerJobResource, 0)
for _, resource := range resources {
apiResources = append(apiResources, convertProvisionerJobResource(resource))
}
render.Status(r, http.StatusOK)
render.JSON(rw, r, resources)
render.JSON(rw, r, apiResources)
}
18 changes: 17 additions & 1 deletion coderd/provisionerjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ type ProvisionerJobResource struct {
ID uuid.UUID `json:"id"`
CreatedAt time.Time `json:"created_at"`
JobID uuid.UUID `json:"job_id"`
Transition database.WorkspaceTransition `json:"workspace_transition"`
Transition database.WorkspaceTransition `json:"transition"`
Type string `json:"type"`
Name string `json:"name"`
AgentID *uuid.UUID `json:"agent_id,omitempty"`
}

type ProvisionerJobAgent struct {
Expand Down Expand Up @@ -291,6 +292,21 @@ func convertProvisionerJob(provisionerJob database.ProvisionerJob) ProvisionerJo
return job
}

func convertProvisionerJobResource(resource database.ProvisionerJobResource) ProvisionerJobResource {
apiResource := ProvisionerJobResource{
ID: resource.ID,
CreatedAt: resource.CreatedAt,
JobID: resource.JobID,
Transition: resource.Transition,
Type: resource.Type,
Name: resource.Name,
}
if resource.AgentID.Valid {
apiResource.AgentID = &resource.AgentID.UUID
}
return apiResource
}

func provisionerJobLogsChannel(jobID uuid.UUID) string {
return fmt.Sprintf("provisioner-log-logs:%s", jobID)
}
65 changes: 47 additions & 18 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/mapstructure"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -75,10 +76,13 @@ func (t *terraform) Provision(request *proto.Provision_Request, stream proto.DRP
}

func (t *terraform) runTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, request *proto.Provision_Request, stream proto.DRPCProvisioner_ProvisionStream) error {
env := map[string]string{
"CODER_URL": request.Metadata.CoderUrl,
"CODER_WORKSPACE_TRANSITION": strings.ToLower(request.Metadata.WorkspaceTransition.String()),
env := map[string]string{}
for _, envEntry := range os.Environ() {
parts := strings.SplitN(envEntry, "=", 2)
env[parts[0]] = parts[1]
}
env["CODER_URL"] = request.Metadata.CoderUrl
env["CODER_WORKSPACE_TRANSITION"] = strings.ToLower(request.Metadata.WorkspaceTransition.String())
planfilePath := filepath.Join(request.Directory, "terraform.tfplan")
options := []tfexec.PlanOption{tfexec.JSON(true), tfexec.Out(planfilePath)}
for _, param := range request.ParameterValues {
Expand Down Expand Up @@ -159,9 +163,31 @@ func (t *terraform) runTerraformPlan(ctx context.Context, terraform *tfexec.Terr
_ = reader.Close()
<-closeChan

// Maps resource dependencies to expression references.
// This is *required* for a plan, because "DependsOn"
// does not propagate.
resourceDependencies := map[string][]string{}
for _, resource := range plan.Config.RootModule.Resources {
if resource.Expressions == nil {
resource.Expressions = map[string]*tfjson.Expression{}
}
// Count expression is separated for logical reasons,
// but it's simpler syntactically for us to combine here.
if resource.CountExpression != nil {
resource.Expressions["count"] = resource.CountExpression
}
for _, expression := range resource.Expressions {
dependencies, exists := resourceDependencies[resource.Address]
if !exists {
dependencies = []string{}
}
dependencies = append(dependencies, expression.References...)
resourceDependencies[resource.Address] = dependencies
}
}

resources := make([]*proto.Resource, 0)
agents := map[string]*proto.Agent{}
agentDepends := map[string][]string{}

// Store all agents inside the maps!
for _, resource := range plan.Config.RootModule.Resources {
Expand Down Expand Up @@ -209,31 +235,33 @@ func (t *terraform) runTerraformPlan(ctx context.Context, terraform *tfexec.Terr
}
}

resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
agents[resourceKey] = agent
agentDepends[resourceKey] = resource.DependsOn
agents[resource.Address] = agent
}

for _, resource := range plan.Config.RootModule.Resources {
for _, resource := range plan.PlannedValues.RootModule.Resources {
if resource.Type == "coder_agent" {
continue
}
// The resource address on planned values can include the indexed
// value like "[0]", but the config doesn't have these, and we don't
// care which index the resource is.
resourceAddress := fmt.Sprintf("%s.%s", resource.Type, resource.Name)
var agent *proto.Agent
// Associate resources that depend on an agent.
for _, dep := range resource.DependsOn {
for _, dependency := range resourceDependencies[resourceAddress] {
var has bool
agent, has = agents[dep]
agent, has = agents[dependency]
if has {
break
}
}
// Associate resources where the agent depends on it.
for agentKey, dependsOn := range agentDepends {
for _, depend := range dependsOn {
if depend != strings.Join([]string{resource.Type, resource.Name}, ".") {
for agentAddress := range agents {
for _, depend := range resourceDependencies[agentAddress] {
if depend != resource.Address {
continue
}
agent = agents[agentKey]
agent = agents[agentAddress]
break
}
}
Expand All @@ -255,10 +283,11 @@ func (t *terraform) runTerraformPlan(ctx context.Context, terraform *tfexec.Terr
}

func (t *terraform) runTerraformApply(ctx context.Context, terraform *tfexec.Terraform, request *proto.Provision_Request, stream proto.DRPCProvisioner_ProvisionStream, statefilePath string) error {
env := []string{
"CODER_URL=" + request.Metadata.CoderUrl,
"CODER_WORKSPACE_TRANSITION=" + strings.ToLower(request.Metadata.WorkspaceTransition.String()),
}
env := os.Environ()
env = append(env,
"CODER_URL="+request.Metadata.CoderUrl,
"CODER_WORKSPACE_TRANSITION="+strings.ToLower(request.Metadata.WorkspaceTransition.String()),
)
vars := []string{}
for _, param := range request.ParameterValues {
switch param.DestinationScheme {
Expand Down
9 changes: 3 additions & 6 deletions provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,10 @@ provider "coder" {
Files: map[string]string{
"main.tf": provider + `
resource "coder_agent" "A" {
count = 1
}
resource "null_resource" "A" {
depends_on = [
coder_agent.A
]
count = length(coder_agent.A)
}`,
},
Request: &proto.Provision_Request{
Expand All @@ -266,9 +265,7 @@ provider "coder" {
Files: map[string]string{
"main.tf": provider + `
resource "coder_agent" "A" {
depends_on = [
null_resource.A
]
count = length(null_resource.A)
auth {
type = "google-instance-identity"
instance_id = "an-instance"
Expand Down