@@ -6,6 +6,7 @@ package provider
66import (
77 "context"
88 "fmt"
9+ "strings"
910
1011 "github.com/CircleCI-Public/circleci-sdk-go/project"
1112 "github.com/hashicorp/terraform-plugin-framework/attr"
2425type projectResourceModel struct {
2526 Id types.String `tfsdk:"id"`
2627 Name types.String `tfsdk:"name"`
27- Provider types.String `tfsdk:"project_provider"`
2828 Slug types.String `tfsdk:"slug"`
2929 OrganizationName types.String `tfsdk:"organization_name"`
3030 OrganizationSlug types.String `tfsdk:"organization_slug"`
@@ -70,10 +70,6 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
7070 MarkdownDescription : "name of the circleci project" ,
7171 Required : true ,
7272 },
73- "project_provider" : schema.StringAttribute {
74- MarkdownDescription : "provider of the circleci project (usually `circleci`)" ,
75- Required : true ,
76- },
7773 "slug" : schema.StringAttribute {
7874 MarkdownDescription : "slug of the circleci project " ,
7975 Computed : true ,
@@ -201,7 +197,6 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
201197 // Map response body to schema and populate Computed attribute values
202198 circleCiTerrformProjectResource .Id = types .StringValue (newCreatedProject .Id )
203199 circleCiTerrformProjectResource .Name = types .StringValue (newCreatedProject .Name )
204- // provider is set in the state and is not brought by the API
205200 circleCiTerrformProjectResource .Slug = types .StringValue (newCreatedProject .Slug )
206201 circleCiTerrformProjectResource .OrganizationName = types .StringValue (newCreatedProject .OrganizationName )
207202 circleCiTerrformProjectResource .OrganizationSlug = types .StringValue (newCreatedProject .OrganizationSlug )
@@ -210,55 +205,40 @@ func (r *projectResource) Create(ctx context.Context, req resource.CreateRequest
210205 circleCiTerrformProjectResource .VcsInfoProvider = types .StringValue (newCreatedProject .VcsInfo .Provider )
211206 circleCiTerrformProjectResource .VcsInfoDefaultBranch = types .StringValue (newCreatedProject .VcsInfo .DefaultBranch )
212207
213- if circleCiTerrformProjectResource .Provider .ValueString () == "circleci" {
214- newProjectSettings , err := r .client .UpdateSettings (
215- project.ProjectSettings {Advanced : newAdvancedSettings },
216- circleCiTerrformProjectResource .Provider .ValueString (),
217- circleCiTerrformProjectResource .OrganizationId .ValueString (),
218- newCreatedProject .Id ,
219- )
220- if err != nil {
221- resp .Diagnostics .AddError (
222- "Error updating CircleCI project settings" ,
223- fmt .Sprintf ("Could not update CircleCI project settings:\n \n settings: %+v\n provider: %s\n org: %s\n project_id: %s\n project_name: %s\n \n Unexpected error: %s\n " , newAdvancedSettings , circleCiTerrformProjectResource .Provider .ValueString (), circleCiTerrformProjectResource .OrganizationId .ValueString (), newCreatedProject .Id , newCreatedProject .Name , err .Error ()),
224- )
225- return
226- }
208+ slug := strings .Split (newCreatedProject .Slug , "/" )
209+ newProjectSettings , err := r .client .UpdateSettings (
210+ project.ProjectSettings {Advanced : newAdvancedSettings },
211+ slug [0 ],
212+ slug [1 ],
213+ slug [2 ],
214+ )
227215
228- circleCiTerrformProjectResource .AutoCancelBuilds = types .BoolPointerValue (newProjectSettings .Advanced .AutocancelBuilds )
229- circleCiTerrformProjectResource .BuildForkPrs = types .BoolPointerValue (newProjectSettings .Advanced .BuildForkPrs )
230- circleCiTerrformProjectResource .DisableSSH = types .BoolPointerValue (newProjectSettings .Advanced .DisableSSH )
231- circleCiTerrformProjectResource .ForksReceiveSecretEnvVars = types .BoolPointerValue (newProjectSettings .Advanced .ForksReceiveSecretEnvVars )
232- circleCiTerrformProjectResource .OSS = types .BoolPointerValue (newProjectSettings .Advanced .OSS )
233- circleCiTerrformProjectResource .SetGithubStatus = types .BoolPointerValue (newProjectSettings .Advanced .SetGithubStatus )
234- circleCiTerrformProjectResource .SetupWorkflows = types .BoolPointerValue (newProjectSettings .Advanced .SetupWorkflows )
235- circleCiTerrformProjectResource .WriteSettingsRequiresAdmin = types .BoolPointerValue (newProjectSettings .Advanced .WriteSettingsRequiresAdmin )
236-
237- nBranchLength := len (newProjectSettings .Advanced .PROnlyBranchOverrides )
238- listStringValuesBanches := make ([]attr.Value , nBranchLength )
239- for index , elem := range newProjectSettings .Advanced .PROnlyBranchOverrides {
240- listStringValuesBanches [index ] = types .StringValue (elem )
241- }
242- circleCiTerrformProjectResource .PROnlyBranchOverrides , diags = types .ListValue (
243- types .StringType ,
244- listStringValuesBanches ,
245- )
246- } else {
247- circleCiTerrformProjectResource .AutoCancelBuilds = types .BoolValue (false )
248- circleCiTerrformProjectResource .BuildForkPrs = types .BoolValue (false )
249- circleCiTerrformProjectResource .DisableSSH = types .BoolValue (false )
250- circleCiTerrformProjectResource .ForksReceiveSecretEnvVars = types .BoolValue (false )
251- circleCiTerrformProjectResource .OSS = types .BoolValue (false )
252- circleCiTerrformProjectResource .SetGithubStatus = types .BoolValue (false )
253- circleCiTerrformProjectResource .SetupWorkflows = types .BoolValue (false )
254- circleCiTerrformProjectResource .WriteSettingsRequiresAdmin = types .BoolValue (false )
255- nBranchLength := 0
256- listStringValuesBanches := make ([]attr.Value , nBranchLength )
257- circleCiTerrformProjectResource .PROnlyBranchOverrides , diags = types .ListValue (
258- types .StringType ,
259- listStringValuesBanches ,
216+ if err != nil {
217+ resp .Diagnostics .AddError (
218+ "Error updating CircleCI project settings" ,
219+ fmt .Sprintf ("Could not update CircleCI project settings:\n \n settings: %+v\n org: %s\n project_id: %s\n project_name: %s\n slug: %s\n \n Unexpected error: %s\n " , newAdvancedSettings , circleCiTerrformProjectResource .OrganizationId .ValueString (), newCreatedProject .Id , newCreatedProject .Name , newCreatedProject .Slug , err .Error ()),
260220 )
221+ return
222+ }
223+
224+ circleCiTerrformProjectResource .AutoCancelBuilds = types .BoolPointerValue (newProjectSettings .Advanced .AutocancelBuilds )
225+ circleCiTerrformProjectResource .BuildForkPrs = types .BoolPointerValue (newProjectSettings .Advanced .BuildForkPrs )
226+ circleCiTerrformProjectResource .DisableSSH = types .BoolPointerValue (newProjectSettings .Advanced .DisableSSH )
227+ circleCiTerrformProjectResource .ForksReceiveSecretEnvVars = types .BoolPointerValue (newProjectSettings .Advanced .ForksReceiveSecretEnvVars )
228+ circleCiTerrformProjectResource .OSS = types .BoolPointerValue (newProjectSettings .Advanced .OSS )
229+ circleCiTerrformProjectResource .SetGithubStatus = types .BoolPointerValue (newProjectSettings .Advanced .SetGithubStatus )
230+ circleCiTerrformProjectResource .SetupWorkflows = types .BoolPointerValue (newProjectSettings .Advanced .SetupWorkflows )
231+ circleCiTerrformProjectResource .WriteSettingsRequiresAdmin = types .BoolPointerValue (newProjectSettings .Advanced .WriteSettingsRequiresAdmin )
232+
233+ nBranchLength := len (newProjectSettings .Advanced .PROnlyBranchOverrides )
234+ listStringValuesBanches := make ([]attr.Value , nBranchLength )
235+ for index , elem := range newProjectSettings .Advanced .PROnlyBranchOverrides {
236+ listStringValuesBanches [index ] = types .StringValue (elem )
261237 }
238+ circleCiTerrformProjectResource .PROnlyBranchOverrides , diags = types .ListValue (
239+ types .StringType ,
240+ listStringValuesBanches ,
241+ )
262242
263243 resp .Diagnostics .Append (diags ... )
264244 if resp .Diagnostics .HasError () {
@@ -299,7 +279,6 @@ func (r *projectResource) Read(ctx context.Context, req resource.ReadRequest, re
299279 projectState = projectResourceModel {
300280 Id : types .StringValue (apiProject .Id ),
301281 Name : types .StringValue (apiProject .Name ),
302- Provider : projectState .Provider ,
303282 Slug : projectState .Slug ,
304283 OrganizationName : projectState .OrganizationName ,
305284 OrganizationSlug : projectState .OrganizationSlug ,
@@ -309,47 +288,35 @@ func (r *projectResource) Read(ctx context.Context, req resource.ReadRequest, re
309288 VcsInfoDefaultBranch : projectState .VcsInfoDefaultBranch ,
310289 }
311290
312- if apiProject .VcsInfo .Provider == "circleci" {
313- projectSettings , err := r .client .GetSettings (
314- projectState .Provider .ValueString (),
315- projectState .OrganizationId .ValueString (),
316- projectState .Id .ValueString (),
291+ slug := strings .Split (projectState .Slug .ValueString (), "/" )
292+ projectSettings , err := r .client .GetSettings (
293+ slug [0 ],
294+ slug [1 ],
295+ slug [2 ],
296+ )
297+
298+ if err != nil {
299+ resp .Diagnostics .AddError (
300+ "Unable to Read CircleCI project settings" ,
301+ err .Error (),
317302 )
318- if err != nil {
319- resp .Diagnostics .AddError (
320- "Unable to Read CircleCI project settings" ,
321- err .Error (),
322- )
323- return
324- }
325- projectState .AutoCancelBuilds = types .BoolPointerValue (projectSettings .Advanced .AutocancelBuilds )
326- projectState .BuildForkPrs = types .BoolPointerValue (projectSettings .Advanced .BuildForkPrs )
327- projectState .DisableSSH = types .BoolPointerValue (projectSettings .Advanced .DisableSSH )
328- projectState .ForksReceiveSecretEnvVars = types .BoolPointerValue (projectSettings .Advanced .ForksReceiveSecretEnvVars )
329- projectState .OSS = types .BoolPointerValue (projectSettings .Advanced .OSS )
330- projectState .SetGithubStatus = types .BoolPointerValue (projectSettings .Advanced .SetGithubStatus )
331- projectState .SetupWorkflows = types .BoolPointerValue (projectSettings .Advanced .SetupWorkflows )
332- projectState .WriteSettingsRequiresAdmin = types .BoolPointerValue (projectSettings .Advanced .WriteSettingsRequiresAdmin )
333-
334- pROnlyBranchOverridesAttributeValues := make ([]attr.Value , len (projectSettings .Advanced .PROnlyBranchOverrides ))
335- for index , elem := range projectSettings .Advanced .PROnlyBranchOverrides {
336- pROnlyBranchOverridesAttributeValues [index ] = types .StringValue (elem )
337- }
338- PROnlyBranchOverridesListValue , _ := types .ListValue (types .StringType , pROnlyBranchOverridesAttributeValues )
339- projectState .PROnlyBranchOverrides = PROnlyBranchOverridesListValue
340- } else {
341- projectState .AutoCancelBuilds = types .BoolValue (false )
342- projectState .BuildForkPrs = types .BoolValue (false )
343- projectState .DisableSSH = types .BoolValue (false )
344- projectState .ForksReceiveSecretEnvVars = types .BoolValue (false )
345- projectState .OSS = types .BoolValue (false )
346- projectState .SetGithubStatus = types .BoolValue (false )
347- projectState .SetupWorkflows = types .BoolValue (false )
348- projectState .WriteSettingsRequiresAdmin = types .BoolValue (false )
349- pROnlyBranchOverridesAttributeValues := make ([]attr.Value , 0 )
350- PROnlyBranchOverridesListValue , _ := types .ListValue (types .StringType , pROnlyBranchOverridesAttributeValues )
351- projectState .PROnlyBranchOverrides = PROnlyBranchOverridesListValue
303+ return
304+ }
305+ projectState .AutoCancelBuilds = types .BoolPointerValue (projectSettings .Advanced .AutocancelBuilds )
306+ projectState .BuildForkPrs = types .BoolPointerValue (projectSettings .Advanced .BuildForkPrs )
307+ projectState .DisableSSH = types .BoolPointerValue (projectSettings .Advanced .DisableSSH )
308+ projectState .ForksReceiveSecretEnvVars = types .BoolPointerValue (projectSettings .Advanced .ForksReceiveSecretEnvVars )
309+ projectState .OSS = types .BoolPointerValue (projectSettings .Advanced .OSS )
310+ projectState .SetGithubStatus = types .BoolPointerValue (projectSettings .Advanced .SetGithubStatus )
311+ projectState .SetupWorkflows = types .BoolPointerValue (projectSettings .Advanced .SetupWorkflows )
312+ projectState .WriteSettingsRequiresAdmin = types .BoolPointerValue (projectSettings .Advanced .WriteSettingsRequiresAdmin )
313+
314+ pROnlyBranchOverridesAttributeValues := make ([]attr.Value , len (projectSettings .Advanced .PROnlyBranchOverrides ))
315+ for index , elem := range projectSettings .Advanced .PROnlyBranchOverrides {
316+ pROnlyBranchOverridesAttributeValues [index ] = types .StringValue (elem )
352317 }
318+ PROnlyBranchOverridesListValue , _ := types .ListValue (types .StringType , pROnlyBranchOverridesAttributeValues )
319+ projectState .PROnlyBranchOverrides = PROnlyBranchOverridesListValue
353320
354321 // Set state
355322 diags := resp .State .Set (ctx , & projectState )
0 commit comments