|
8 | 8 | "strings" |
9 | 9 |
|
10 | 10 | "github.com/google/go-github/v83/github" |
| 11 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
11 | 12 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
12 | 13 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" |
13 | 14 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
@@ -158,36 +159,46 @@ func resourceGithubRepositoryPagesCreate(ctx context.Context, d *schema.Resource |
158 | 159 | return diag.FromErr(err) |
159 | 160 | } |
160 | 161 |
|
161 | | - // Determine if we need to update the page with CNAME or public flag |
162 | | - shouldUpdatePage := false |
163 | | - update := &github.PagesUpdate{} |
164 | | - cname, cnameExists := d.GetOk("cname") |
165 | | - if cnameExists && cname.(string) != "" { |
166 | | - shouldUpdatePage = true |
167 | | - update.CNAME = github.Ptr(cname.(string)) |
168 | | - } |
169 | | - public, publicExists := d.GetOkExists("public") // nolint:staticcheck // SA1019: There is no better alternative for checking if boolean value is set |
170 | | - if publicExists && public != nil { |
171 | | - shouldUpdatePage = true |
172 | | - update.Public = github.Ptr(public.(bool)) |
173 | | - } else { |
174 | | - if err := d.Set("public", pages.GetPublic()); err != nil { |
| 162 | + // Check if we have values set that can't be configured as part of the create logic |
| 163 | + cname, cnameOK := d.GetOk("cname") |
| 164 | + public, publicOKExists := d.GetOkExists("public") //nolint:staticcheck // SA1019: d.GetOkExists is deprecated but necessary for bool fields |
| 165 | + httpsEnforced, httpsEnforcedExists := d.GetOkExists("https_enforced") //nolint:staticcheck // SA1019: d.GetOkExists is deprecated but necessary for bool fields |
| 166 | + tflog.Debug(ctx, "Do we have values set that need the update logic?", map[string]any{ |
| 167 | + "public": public, |
| 168 | + "public_ok_exists": publicOKExists, |
| 169 | + "https_enforced": httpsEnforced, |
| 170 | + "https_enforced_exists": httpsEnforcedExists, |
| 171 | + "cname": cname, |
| 172 | + "cname_ok": cnameOK, |
| 173 | + }) |
| 174 | + |
| 175 | + if cnameOK || publicOKExists || httpsEnforcedExists { |
| 176 | + update := &github.PagesUpdate{} |
| 177 | + |
| 178 | + if cnameOK { |
| 179 | + update.CNAME = github.Ptr(cname.(string)) |
| 180 | + } |
| 181 | + |
| 182 | + if publicOKExists { |
| 183 | + update.Public = github.Ptr(public.(bool)) |
| 184 | + } |
| 185 | + |
| 186 | + if httpsEnforcedExists { |
| 187 | + update.HTTPSEnforced = github.Ptr(httpsEnforced.(bool)) |
| 188 | + } |
| 189 | + |
| 190 | + _, err = client.Repositories.UpdatePages(ctx, owner, repoName, update) |
| 191 | + if err != nil { |
175 | 192 | return diag.FromErr(err) |
176 | 193 | } |
177 | 194 | } |
178 | | - httpsEnforced, httpsEnforcedExists := d.GetOkExists("https_enforced") // nolint:staticcheck // SA1019: There is no better alternative for checking if boolean value is set |
179 | | - if httpsEnforcedExists && httpsEnforced != nil { |
180 | | - shouldUpdatePage = true |
181 | | - update.HTTPSEnforced = github.Ptr(httpsEnforced.(bool)) |
182 | | - } else { |
183 | | - if err := d.Set("https_enforced", pages.GetHTTPSEnforced()); err != nil { |
| 195 | + if !publicOKExists { |
| 196 | + if err := d.Set("public", pages.GetPublic()); err != nil { |
184 | 197 | return diag.FromErr(err) |
185 | 198 | } |
186 | 199 | } |
187 | | - |
188 | | - if shouldUpdatePage { |
189 | | - _, err = client.Repositories.UpdatePages(ctx, owner, repoName, update) |
190 | | - if err != nil { |
| 200 | + if !httpsEnforcedExists { |
| 201 | + if err := d.Set("https_enforced", pages.GetHTTPSEnforced()); err != nil { |
191 | 202 | return diag.FromErr(err) |
192 | 203 | } |
193 | 204 | } |
|
0 commit comments