-
Notifications
You must be signed in to change notification settings - Fork 899
fix: Add tests for instance and app association #2198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This was regressed in #2187. There was bad testing around this before, and this should prevent a similiar situation from happening again!
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,17 +120,86 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res | |
} | ||
} | ||
|
||
type appAttributes struct { | ||
AgentID string `mapstructure:"agent_id"` | ||
Name string `mapstructure:"name"` | ||
Icon string `mapstructure:"icon"` | ||
URL string `mapstructure:"url"` | ||
Command string `mapstructure:"command"` | ||
RelativePath bool `mapstructure:"relative_path"` | ||
} | ||
// Associate Apps with agents. | ||
for _, resource := range tfResources { | ||
if resource.Type != "coder_app" { | ||
continue | ||
} | ||
var attrs appAttributes | ||
err = mapstructure.Decode(resource.AttributeValues, &attrs) | ||
if err != nil { | ||
return nil, xerrors.Errorf("decode app attributes: %w", err) | ||
} | ||
if attrs.Name == "" { | ||
// Default to the resource name if none is set! | ||
attrs.Name = resource.Name | ||
} | ||
for _, agent := range agents { | ||
if agent.Id != attrs.AgentID { | ||
continue | ||
} | ||
agent.Apps = append(agent.Apps, &proto.App{ | ||
Name: attrs.Name, | ||
Command: attrs.Command, | ||
Url: attrs.URL, | ||
Icon: attrs.Icon, | ||
RelativePath: attrs.RelativePath, | ||
}) | ||
} | ||
} | ||
|
||
for _, resource := range tfResources { | ||
if resource.Mode == tfjson.DataResourceMode { | ||
continue | ||
} | ||
if resource.Type == "coder_agent" || resource.Type == "coder_agent_instance" || resource.Type == "coder_app" { | ||
continue | ||
} | ||
agents := findAgents(resourceDependencies, agents, convertAddressToLabel(resource.Address)) | ||
for _, agent := range agents { | ||
// Didn't use instance identity. | ||
if agent.GetToken() != "" { | ||
continue | ||
} | ||
|
||
// These resource types are for automatically associating an instance ID | ||
// with an agent for authentication. | ||
key, isValid := map[string]string{ | ||
"google_compute_instance": "instance_id", | ||
"aws_instance": "id", | ||
"azurerm_linux_virtual_machine": "id", | ||
"azurerm_windows_virtual_machine": "id", | ||
}[resource.Type] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I'm still pretty noobish when it comes to Terraform, but it seems like we're automatically mapping these resource types for all agents. So for instance if there's both a I'm sure the above wouldn't be a common use case, but I imagine it would be possible to create a semi-complex Terraform template with multiple providers depending on certain selections? And I imagine this would be a use-case for having multiple agents, are there others? If my questions make no sense, feel free to disregard 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
All questions I'm happy to answer!
There's a one -> many mapping of resource to agent. If a resource isn't using token authentication, we assume it's using the zero-trust providers we support: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, thanks for clarifying @kylecarbs! I don't think we need to make any changes to it at this point, but good to be aware of it. |
||
if !isValid { | ||
// The resource type doesn't support | ||
// automatically setting the instance ID. | ||
continue | ||
} | ||
instanceIDRaw, valid := resource.AttributeValues[key] | ||
if !valid { | ||
continue | ||
} | ||
instanceID, valid := instanceIDRaw.(string) | ||
if !valid { | ||
continue | ||
} | ||
agent.Auth = &proto.Agent_InstanceId{ | ||
InstanceId: instanceID, | ||
} | ||
} | ||
|
||
resources = append(resources, &proto.Resource{ | ||
Name: resource.Name, | ||
Type: resource.Type, | ||
Agents: findAgents(resourceDependencies, agents, convertAddressToLabel(resource.Address)), | ||
Agents: agents, | ||
}) | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
terraform { | ||
required_providers { | ||
coder = { | ||
source = "coder/coder" | ||
version = "0.4.2" | ||
} | ||
} | ||
} | ||
|
||
resource "coder_agent" "dev1" { | ||
os = "linux" | ||
arch = "amd64" | ||
} | ||
|
||
resource "coder_app" "app1" { | ||
agent_id = coder_agent.dev1.id | ||
} | ||
|
||
resource "coder_app" "app2" { | ||
agent_id = coder_agent.dev1.id | ||
} | ||
|
||
resource "null_resource" "dev" { | ||
depends_on = [ | ||
coder_agent.dev1 | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.