|
| 1 | +package rancher2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 8 | + managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceRancher2AuthConfigGithubApp() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Create: resourceRancher2AuthConfigGithubAppCreate, |
| 14 | + Read: resourceRancher2AuthConfigGithubAppRead, |
| 15 | + Update: resourceRancher2AuthConfigGithubAppUpdate, |
| 16 | + Delete: resourceRancher2AuthConfigGithubAppDelete, |
| 17 | + Schema: authConfigGithubAppFields(), |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func resourceRancher2AuthConfigGithubAppCreate(d *schema.ResourceData, meta interface{}) error { |
| 22 | + client, err := meta.(*Config).ManagementClient() |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("[ERROR] Failed to get ManagementClient %s", err) |
| 25 | + } |
| 26 | + |
| 27 | + auth, err := client.AuthConfig.ByID(AuthConfigGithubAppName) |
| 28 | + if err != nil { |
| 29 | + return fmt.Errorf("[ERROR] Failed to get Auth Config %s: %s", AuthConfigGithubAppName, err) |
| 30 | + } |
| 31 | + |
| 32 | + log.Printf("[INFO] Creating Auth Config %s %s", AuthConfigGithubAppName, auth.Name) |
| 33 | + |
| 34 | + authGithubApp, err := expandAuthConfigGithubApp(d) |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("[ERROR] Failed expanding Auth Config %s: %s", AuthConfigGithubAppName, err) |
| 37 | + } |
| 38 | + |
| 39 | + // Checking if other auth config is enabled |
| 40 | + if authGithubApp.Enabled { |
| 41 | + err = meta.(*Config).CheckAuthConfigEnabled(AuthConfigGithubAppName) |
| 42 | + if err != nil { |
| 43 | + return fmt.Errorf("[ERROR] Checking if an Auth Config other than %s is enabled: %s", AuthConfigGithubAppName, err) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Updated auth config |
| 48 | + newAuth := &managementClient.GithubAppConfig{} |
| 49 | + err = meta.(*Config).UpdateAuthConfig(auth.Links["self"], authGithubApp, newAuth) |
| 50 | + if err != nil { |
| 51 | + return fmt.Errorf("[ERROR] Updating Auth Config %s: %s", AuthConfigGithubAppName, err) |
| 52 | + } |
| 53 | + |
| 54 | + return resourceRancher2AuthConfigGithubAppRead(d, meta) |
| 55 | +} |
| 56 | + |
| 57 | +func resourceRancher2AuthConfigGithubAppRead(d *schema.ResourceData, meta interface{}) error { |
| 58 | + log.Printf("[INFO] Refreshing Auth Config %s", AuthConfigGithubAppName) |
| 59 | + client, err := meta.(*Config).ManagementClient() |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("[ERROR] Failed to get ManagementClient %s", err) |
| 62 | + } |
| 63 | + |
| 64 | + auth, err := client.AuthConfig.ByID(AuthConfigGithubAppName) |
| 65 | + if err != nil { |
| 66 | + if IsNotFound(err) { |
| 67 | + log.Printf("[INFO] Auth Config %s not found.", AuthConfigGithubAppName) |
| 68 | + d.SetId("") |
| 69 | + return nil |
| 70 | + } |
| 71 | + return fmt.Errorf("[ERROR] Getting Auth Config %s By ID: %w", AuthConfigGithubAppName, err) |
| 72 | + } |
| 73 | + |
| 74 | + authGithubApp, err := meta.(*Config).GetAuthConfig(auth) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("[ERROR] Getting Auth Config %s: %w", AuthConfigGithubAppName, err) |
| 77 | + } |
| 78 | + |
| 79 | + err = flattenAuthConfigGithubApp(d, authGithubApp.(*managementClient.GithubAppConfig)) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("[ERROR] Flattening GitHub app: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func resourceRancher2AuthConfigGithubAppUpdate(d *schema.ResourceData, meta interface{}) error { |
| 88 | + log.Printf("[INFO] Updating Auth Config %s", AuthConfigGithubAppName) |
| 89 | + |
| 90 | + return resourceRancher2AuthConfigGithubAppCreate(d, meta) |
| 91 | +} |
| 92 | + |
| 93 | +func resourceRancher2AuthConfigGithubAppDelete(d *schema.ResourceData, meta interface{}) error { |
| 94 | + log.Printf("[INFO] Disabling Auth Config %s", AuthConfigGithubAppName) |
| 95 | + |
| 96 | + client, err := meta.(*Config).ManagementClient() |
| 97 | + if err != nil { |
| 98 | + return fmt.Errorf("[ERROR] Failed to get ManagementClient %s", err) |
| 99 | + } |
| 100 | + |
| 101 | + auth, err := client.AuthConfig.ByID(AuthConfigGithubAppName) |
| 102 | + if err != nil { |
| 103 | + if IsNotFound(err) { |
| 104 | + log.Printf("[INFO] Auth Config %s not found.", AuthConfigGithubAppName) |
| 105 | + d.SetId("") |
| 106 | + return nil |
| 107 | + } |
| 108 | + return fmt.Errorf("[ERROR] Getting the %s AuthConfig: %w", AuthConfigGithubAppName, err) |
| 109 | + } |
| 110 | + |
| 111 | + if auth.Enabled == true { |
| 112 | + err = client.Post(auth.Actions["disable"], nil, nil) |
| 113 | + if err != nil { |
| 114 | + return fmt.Errorf("[ERROR] Disabling Auth Config %s: %s", AuthConfigGithubAppName, err) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + d.SetId("") |
| 119 | + return nil |
| 120 | +} |
0 commit comments