Skip to content
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

PR #147: Machine User Role and Resource Role Assignment Fails #147

Merged
merged 1 commit into from
Jul 31, 2024

Conversation

ueisele
Copy link
Contributor

@ueisele ueisele commented Jul 29, 2024

We want to assign a role to a machine user via the Terraform resource cdp_iam_machine_user_role_assignment:

resource "cdp_iam_machine_user" "provisioning" {
  name = "dev-provisioning"
}

resource "cdp_iam_machine_user_role_assignment" "provisioning_environment_admin" {
  machine_user = cdp_iam_machine_user.provisioning.name
  role                   = "crn:altus:iam:eu-1:altus:role:EnvironmentAdmin"

  depends_on = [ cdp_iam_machine_user.provisioning ]
}

When applying the changes, Terraform fails with a nil pointer dereference error:

cdp_iam_machine_user_role_assignment.provisioning_environment_admin: Creating...
╷
│ Error: Request cancelled
│
│ The plugin6.(*GRPCProvider).ApplyResourceChange request was cancelled.
╵
 
Stack trace from the terraform-provider-cdp_v0.6.2 plugin:
 
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0xea1cc2]
 
goroutine 83 [running]:
github.com/cloudera/terraform-provider-cdp/resources/iam.(*machineUserRoleAssignmentResource).Create(0xc000118740, {0x16eff28, 0xc0003507e0}, {{{{0x16f6478, 0xc000350f00}, {0x129c4e0, 0xc000350e10}}, {0x16f84e8, 0xc0004a6be0}}, {{{0x16f6478, ...}, ...}, ...}, ...}, ...)
        github.com/cloudera/terraform-provider-cdp/resources/iam/resource_machine_user_role_assignment.go:59 +0x362
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).CreateResource(0xc00041c9c0, {0x16eff28, 0xc0003507e0}, 0xc0005315e0, 0xc0005315b8)
        github.com/hashicorp/[email protected]/internal/fwserver/server_createresource.go:101 +0x578
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ApplyResourceChange(0xc00041c9c0, {0x16eff28, 0xc0003507e0}, 0xc0002a3360, 0xc0005316d0)
        github.com/hashicorp/[email protected]/internal/fwserver/server_applyresourcechange.go:57 +0x4aa
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ApplyResourceChange(0xc00041c9c0, {0x16eff28?, 0xc0003506f0?}, 0xc0002a32c0)
        github.com/hashicorp/[email protected]/internal/proto6server/server_applyresourcechange.go:55 +0x38e
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ApplyResourceChange(0xc00043fd60, {0x16eff28?, 0xc000497ce0?}, 0xc000411b90)
        github.com/hashicorp/[email protected]/tfprotov6/tf6server/server.go:846 +0x3d0
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ApplyResourceChange_Handler({0x14c3700, 0xc00043fd60}, {0x16eff28, 0xc000497ce0}, 0xc000433000, 0x0)
        github.com/hashicorp/[email protected]/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:518 +0x1a6
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000428000, {0x16eff28, 0xc000497c50}, {0x16f6ce0, 0xc0001741a0}, 0xc000332d80, 0xc00050c960, 0x2311498, 0x0)
        google.golang.org/[email protected]/server.go:1386 +0xdf8
google.golang.org/grpc.(*Server).handleStream(0xc000428000, {0x16f6ce0, 0xc0001741a0}, 0xc000332d80)
        google.golang.org/[email protected]/server.go:1797 +0xe87
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/[email protected]/server.go:1027 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 27
        google.golang.org/[email protected]/server.go:1038 +0x125
 
Error: The terraform-provider-cdp_v0.6.2 plugin crashed!
 
This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.

The reason for this is, that for this resource, the CDP client has not been initialized. Basically the following code was missing:

r.client = utils.GetCdpClientForResource(req, resp)

After fixing this, applying the change still failed:

cdp_iam_machine_user_role_assignment.provisioning_environment_admin: Creating...
╷
│ Error: Provider returned invalid result object after apply
│
│ After the apply operation, the provider still indicated an unknown value for cdp_iam_machine_user_role_assignment.provisioning_environment_admin.id. All values must be known after apply, so this is always a bug in the provider and should be reported in the provider's own repository. Terraform will still save the other known object values in the state.
╵

It showed that the reason is, that the cdp_iam_machine_user_role_assignment does not assign an id to the response.
After assigning an id with the following code it worked:

data.Id = types.StringValue(data.MachineUser.ValueString() + "_" + data.Role.ValueString())

The resource cdp_iam_machine_user_resource_role_assignment has the exact same problem, so I fixed both with this PR.

Terraform version: v1.9.3
CDP Provider version: v0.6.2

@ueisele ueisele requested a review from a team as a code owner July 29, 2024 13:49
ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 29, 2024
@ueisele ueisele force-pushed the fix/machine-user-role-assignment branch from 794042c to 8b58aa1 Compare July 29, 2024 14:07
ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 29, 2024
@ueisele ueisele force-pushed the fix/machine-user-role-assignment branch from 8b58aa1 to f6c971d Compare July 29, 2024 14:10
@gregito
Copy link
Contributor

gregito commented Jul 30, 2024

thank you for your contribution!
in general, it looks good to me, though the coverage falls just below the required, so could you please cover the new logic?

ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 31, 2024
…nment and cdp_iam_machine_user_resource_role_assignment
ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 31, 2024
…nment and cdp_iam_machine_user_resource_role_assignment
@ueisele ueisele force-pushed the fix/machine-user-role-assignment branch from 3ceba39 to cd080e3 Compare July 31, 2024 15:07
ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 31, 2024
…nment and cdp_iam_machine_user_resource_role_assignment
@ueisele ueisele force-pushed the fix/machine-user-role-assignment branch from cd080e3 to e7a7873 Compare July 31, 2024 15:09
ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 31, 2024
ueisele added a commit to ueisele/terraform-provider-cdp that referenced this pull request Jul 31, 2024
@ueisele ueisele force-pushed the fix/machine-user-role-assignment branch from 973dcc1 to 002dbeb Compare July 31, 2024 16:36
@ueisele
Copy link
Contributor Author

ueisele commented Jul 31, 2024

@gregito I implemented an acceptance test for each of the resources.

If I have seen it correctly, the GitHub quality gates do not execute acceptance tests. I was able to run them locally.

I think the reason why the last quality gate fails is, that I do not have permissions to push the test coverage to your server.

@gregito
Copy link
Contributor

gregito commented Jul 31, 2024

thank you @ueisele! could you please squash the three commits of yours into one? after that, you can rebase and merge it 🙂
Thanks!

@ueisele ueisele force-pushed the fix/machine-user-role-assignment branch from 002dbeb to 4690c04 Compare July 31, 2024 20:10
@ueisele
Copy link
Contributor Author

ueisele commented Jul 31, 2024

@gregito I squashed it. Could you please merge it? I think I have no permissions to merge. Thanks :)

@gregito gregito merged commit c58dabc into cloudera:main Jul 31, 2024
6 of 7 checks passed
@ueisele ueisele deleted the fix/machine-user-role-assignment branch August 1, 2024 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants