-
Notifications
You must be signed in to change notification settings - Fork 961
feat: add enterprise (inherited) runner group org setting resource #3080
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
Open
coopernetes
wants to merge
1
commit into
integrations:main
Choose a base branch
from
RBC:2768-ent-runner-org-resource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+874
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
383 changes: 383 additions & 0 deletions
383
github/resource_github_organization_inherited_runner_group_settings.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,383 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "net/http" | ||
| "strconv" | ||
|
|
||
| "github.com/google/go-github/v83/github" | ||
| "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" | ||
| ) | ||
|
|
||
| func resourceGithubOrganizationInheritedRunnerGroupSettings() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Description: "Manages organization-level settings for an enterprise Actions runner group inherited by the organization.", | ||
|
|
||
| CreateContext: resourceGithubOrganizationInheritedRunnerGroupSettingsCreate, | ||
| ReadContext: resourceGithubOrganizationInheritedRunnerGroupSettingsRead, | ||
| UpdateContext: resourceGithubOrganizationInheritedRunnerGroupSettingsUpdate, | ||
| DeleteContext: resourceGithubOrganizationInheritedRunnerGroupSettingsDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| StateContext: resourceGithubOrganizationInheritedRunnerGroupSettingsImport, | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "organization": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| Description: "The GitHub organization name.", | ||
| }, | ||
| "enterprise_runner_group_name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The name of the enterprise runner group inherited by the organization.", | ||
| }, | ||
| "runner_group_id": { | ||
| Type: schema.TypeInt, | ||
| Computed: true, | ||
| Description: "The ID of the inherited enterprise runner group in the organization.", | ||
| }, | ||
| "inherited": { | ||
| Type: schema.TypeBool, | ||
| Computed: true, | ||
| Description: "Whether this runner group is inherited from the enterprise.", | ||
| }, | ||
| "visibility": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Default: "selected", | ||
| Description: "The visibility of the runner group. Can be 'all', 'selected', or 'private'.", | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"all", "selected", "private"}, false)), | ||
| }, | ||
| "selected_repository_ids": { | ||
| Type: schema.TypeSet, | ||
| Elem: &schema.Schema{ | ||
| Type: schema.TypeInt, | ||
| }, | ||
| Set: schema.HashInt, | ||
| Optional: true, | ||
| Description: "List of repository IDs that can access the runner group. Only applicable when visibility is set to 'selected'.", | ||
| }, | ||
| "allows_public_repositories": { | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| Default: false, | ||
| Description: "Whether public repositories can be added to the runner group.", | ||
| }, | ||
| "restricted_to_workflows": { | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| Default: false, | ||
| Description: "If 'true', the runner group will be restricted to running only the workflows specified in the 'selected_workflows' array. Defaults to 'false'.", | ||
| }, | ||
| "selected_workflows": { | ||
| Type: schema.TypeList, | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| Optional: true, | ||
| Description: "List of workflows the runner group should be allowed to run. This setting will be ignored unless restricted_to_workflows is set to 'true'.", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func findInheritedEnterpriseRunnerGroupByName(client *github.Client, ctx context.Context, org string, name string) (*github.RunnerGroup, error) { | ||
| opts := &github.ListOrgRunnerGroupOptions{ | ||
| ListOptions: github.ListOptions{ | ||
| PerPage: maxPerPage, | ||
| }, | ||
| } | ||
|
|
||
| for { | ||
| groups, resp, err := client.Actions.ListOrganizationRunnerGroups(ctx, org, opts) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| for _, group := range groups.RunnerGroups { | ||
| if group.GetInherited() && group.GetName() == name { | ||
| return group, nil | ||
| } | ||
| } | ||
|
|
||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
| opts.Page = resp.NextPage | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("inherited enterprise runner group '%s' not found in organization '%s'. Ensure the enterprise runner group is shared with this organization", name, org) | ||
| } | ||
|
|
||
| func resourceGithubOrganizationInheritedRunnerGroupSettingsCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
|
|
||
| org := d.Get("organization").(string) | ||
| enterpriseRunnerGroupName := d.Get("enterprise_runner_group_name").(string) | ||
| visibility := d.Get("visibility").(string) | ||
| allowsPublicRepositories := d.Get("allows_public_repositories").(bool) | ||
| restrictedToWorkflows := d.Get("restricted_to_workflows").(bool) | ||
| selectedRepositories, hasSelectedRepositories := d.GetOk("selected_repository_ids") | ||
|
|
||
| selectedWorkflows := []string{} | ||
| if workflows, ok := d.GetOk("selected_workflows"); ok { | ||
| for _, workflow := range workflows.([]any) { | ||
| selectedWorkflows = append(selectedWorkflows, workflow.(string)) | ||
| } | ||
| } | ||
|
|
||
| // Find the inherited enterprise runner group by name | ||
| runnerGroup, err := findInheritedEnterpriseRunnerGroupByName(client, ctx, org, enterpriseRunnerGroupName) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| runnerGroupID := runnerGroup.GetID() | ||
| id, err := buildID(org, strconv.FormatInt(runnerGroupID, 10)) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| d.SetId(id) | ||
|
|
||
| if err := d.Set("runner_group_id", int(runnerGroupID)); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| if err := d.Set("inherited", runnerGroup.GetInherited()); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| // Update runner group settings | ||
| updateReq := github.UpdateRunnerGroupRequest{ | ||
| Visibility: github.Ptr(visibility), | ||
| AllowsPublicRepositories: github.Ptr(allowsPublicRepositories), | ||
| RestrictedToWorkflows: github.Ptr(restrictedToWorkflows), | ||
| SelectedWorkflows: selectedWorkflows, | ||
| } | ||
|
|
||
| _, _, err = client.Actions.UpdateOrganizationRunnerGroup(ctx, org, runnerGroupID, updateReq) | ||
| if err != nil { | ||
| return diag.Errorf("failed to update runner group: %s", err) | ||
| } | ||
|
|
||
| // Set repository access if visibility is "selected" | ||
| if visibility == "selected" && hasSelectedRepositories { | ||
| selectedRepositoryIDs := []int64{} | ||
| for _, id := range selectedRepositories.(*schema.Set).List() { | ||
| selectedRepositoryIDs = append(selectedRepositoryIDs, int64(id.(int))) | ||
| } | ||
|
|
||
| repoAccessReq := github.SetRepoAccessRunnerGroupRequest{ | ||
| SelectedRepositoryIDs: selectedRepositoryIDs, | ||
| } | ||
|
|
||
| _, err = client.Actions.SetRepositoryAccessRunnerGroup(ctx, org, runnerGroupID, repoAccessReq) | ||
| if err != nil { | ||
| return diag.Errorf("failed to set repository access: %s", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubOrganizationInheritedRunnerGroupSettingsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
|
|
||
| org := d.Get("organization").(string) | ||
| runnerGroupID := int64(d.Get("runner_group_id").(int)) | ||
|
|
||
| // Get the runner group details | ||
| runnerGroup, _, err := client.Actions.GetOrganizationRunnerGroup(ctx, org, runnerGroupID) | ||
| if err != nil { | ||
| if ghErr, ok := err.(*github.ErrorResponse); ok { | ||
| if ghErr.Response.StatusCode == http.StatusNotFound { | ||
| log.Printf("[INFO] Removing actions organization runner group %s from state because it no longer exists in GitHub", | ||
|
Collaborator
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. Please refactor to use |
||
| d.Id()) | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
| } | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if err := d.Set("inherited", runnerGroup.GetInherited()); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if err := d.Set("visibility", runnerGroup.GetVisibility()); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| // Get repository access list only if visibility is "selected" | ||
| if runnerGroup.GetVisibility() == "selected" { | ||
| selectedRepositoryIDs := []int64{} | ||
| opts := &github.ListOptions{ | ||
| PerPage: maxPerPage, | ||
| } | ||
|
|
||
| for { | ||
| repos, resp, err := client.Actions.ListRepositoryAccessRunnerGroup(ctx, org, runnerGroupID, opts) | ||
|
Collaborator
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. Does this have the new Iter accessor? Use that if possible |
||
| if err != nil { | ||
| return diag.Errorf("failed to list repository access: %s", err) | ||
| } | ||
|
|
||
| for _, repo := range repos.Repositories { | ||
| selectedRepositoryIDs = append(selectedRepositoryIDs, repo.GetID()) | ||
| } | ||
|
|
||
| if resp.NextPage == 0 { | ||
| break | ||
| } | ||
| opts.Page = resp.NextPage | ||
| } | ||
|
|
||
| if err := d.Set("selected_repository_ids", selectedRepositoryIDs); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } else { | ||
| if err := d.Set("selected_repository_ids", []int64{}); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
| } | ||
|
|
||
| if err := d.Set("allows_public_repositories", runnerGroup.GetAllowsPublicRepositories()); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if err := d.Set("restricted_to_workflows", runnerGroup.GetRestrictedToWorkflows()); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| if err := d.Set("selected_workflows", runnerGroup.SelectedWorkflows); err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubOrganizationInheritedRunnerGroupSettingsUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
|
|
||
| org := d.Get("organization").(string) | ||
| runnerGroupID := int64(d.Get("runner_group_id").(int)) | ||
| visibility := d.Get("visibility").(string) | ||
| selectedRepositories, hasSelectedRepositories := d.GetOk("selected_repository_ids") | ||
|
|
||
| // Update runner group settings if any relevant fields changed | ||
| if d.HasChange("visibility") || d.HasChange("allows_public_repositories") || d.HasChange("restricted_to_workflows") || d.HasChange("selected_workflows") { | ||
| allowsPublicRepositories := d.Get("allows_public_repositories").(bool) | ||
| restrictedToWorkflows := d.Get("restricted_to_workflows").(bool) | ||
|
|
||
| selectedWorkflows := []string{} | ||
| if workflows, ok := d.GetOk("selected_workflows"); ok { | ||
| for _, workflow := range workflows.([]any) { | ||
| selectedWorkflows = append(selectedWorkflows, workflow.(string)) | ||
| } | ||
| } | ||
|
|
||
| updateReq := github.UpdateRunnerGroupRequest{ | ||
| Visibility: github.Ptr(visibility), | ||
| AllowsPublicRepositories: github.Ptr(allowsPublicRepositories), | ||
| RestrictedToWorkflows: github.Ptr(restrictedToWorkflows), | ||
| SelectedWorkflows: selectedWorkflows, | ||
| } | ||
|
|
||
| _, _, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, org, runnerGroupID, updateReq) | ||
| if err != nil { | ||
| return diag.Errorf("failed to update runner group: %s", err) | ||
| } | ||
| } | ||
|
|
||
| // Update repository access if changed and visibility is "selected" | ||
| if d.HasChange("selected_repository_ids") && visibility == "selected" && hasSelectedRepositories { | ||
| selectedRepositoryIDs := []int64{} | ||
|
|
||
| for _, id := range selectedRepositories.(*schema.Set).List() { | ||
| selectedRepositoryIDs = append(selectedRepositoryIDs, int64(id.(int))) | ||
| } | ||
|
|
||
| repoAccessReq := github.SetRepoAccessRunnerGroupRequest{ | ||
| SelectedRepositoryIDs: selectedRepositoryIDs, | ||
| } | ||
|
|
||
| _, err := client.Actions.SetRepositoryAccessRunnerGroup(ctx, org, runnerGroupID, repoAccessReq) | ||
| if err != nil { | ||
| return diag.Errorf("failed to set repository access: %s", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubOrganizationInheritedRunnerGroupSettingsDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
|
|
||
| org := d.Get("organization").(string) | ||
| runnerGroupID := int64(d.Get("runner_group_id").(int)) | ||
|
|
||
| log.Printf("[INFO] Removing repository access for runner group: %s", d.Id()) | ||
|
|
||
| // Reset to "all" visibility and clear repository access | ||
| updateReq := github.UpdateRunnerGroupRequest{ | ||
| Visibility: github.Ptr("all"), | ||
| } | ||
|
|
||
| _, _, err := client.Actions.UpdateOrganizationRunnerGroup(ctx, org, runnerGroupID, updateReq) | ||
| if err != nil { | ||
| // If the runner group doesn't exist, that's fine | ||
| if ghErr, ok := err.(*github.ErrorResponse); ok { | ||
| if ghErr.Response.StatusCode == http.StatusNotFound { | ||
| return nil | ||
| } | ||
| } | ||
| return diag.Errorf("failed to reset runner group visibility: %s", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceGithubOrganizationInheritedRunnerGroupSettingsImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { | ||
| org, identifier, err := parseID2(d.Id()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("invalid import ID format, expected 'organization:enterprise_runner_group_name' or 'organization:organization_runner_group_id'") | ||
| } | ||
|
|
||
| client := meta.(*Owner).v3client | ||
|
|
||
| var runnerGroup *github.RunnerGroup | ||
|
|
||
| // Try to parse as ID first | ||
| if id, parseErr := strconv.ParseInt(identifier, 10, 64); parseErr == nil { | ||
| // It's an ID - get the runner group and verify it's inherited | ||
| runnerGroup, _, err = client.Actions.GetOrganizationRunnerGroup(ctx, org, id) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get runner group: %w", err) | ||
| } | ||
| } else { | ||
| // It's a name - find the inherited enterprise runner group | ||
| runnerGroup, err = findInheritedEnterpriseRunnerGroupByName(client, ctx, org, identifier) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| // Verify the runner group is inherited from the enterprise | ||
| if !runnerGroup.GetInherited() { | ||
| return nil, fmt.Errorf("runner group '%s' is not inherited from the enterprise. This resource only manages inherited enterprise runner groups", runnerGroup.GetName()) | ||
| } | ||
|
|
||
| id, err := buildID(org, strconv.FormatInt(runnerGroup.GetID(), 10)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| d.SetId(id) | ||
| d.Set("organization", org) | ||
| d.Set("enterprise_runner_group_name", runnerGroup.GetName()) | ||
| d.Set("runner_group_id", int(runnerGroup.GetID())) | ||
| d.Set("inherited", runnerGroup.GetInherited()) | ||
|
|
||
| return []*schema.ResourceData{d}, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.