@@ -19,6 +19,10 @@ var _ = registerResource("gitlab_project_mirror", func() *schema.Resource {
1919This is for *pushing* changes to a remote repository. *Pull Mirroring* can be configured using a combination of the
2020import_url, mirror, and mirror_trigger_builds properties on the gitlab_project resource.
2121
22+ -> **Destroy Behavior** GitLab 14.10 introduced an API endpoint to delete a project mirror.
23+ Therefore, for GitLab 14.10 and newer the project mirror will be destroyed when the resource is destroyed.
24+ For older versions, the mirror will be disabled and the resource will be destroyed.
25+
2226**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/ee/api/remote_mirrors.html)` ,
2327
2428 CreateContext : resourceGitlabProjectMirrorCreate ,
@@ -139,29 +143,35 @@ func resourceGitlabProjectMirrorUpdate(ctx context.Context, d *schema.ResourceDa
139143 return resourceGitlabProjectMirrorRead (ctx , d , meta )
140144}
141145
142- // Documented remote mirrors API does not support a delete method, instead mirror is disabled.
143146func resourceGitlabProjectMirrorDelete (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
144147 client := meta .(* gitlab.Client )
145148
146- enabled := false
147-
148149 mirrorID := d .Get ("mirror_id" ).(int )
149150 projectID := d .Get ("project" ).(string )
150- onlyProtectedBranches := d .Get ("only_protected_branches" ).(bool )
151- keepDivergentRefs := d .Get ("keep_divergent_refs" ).(bool )
152151
153- options := gitlab.EditProjectMirrorOptions {
154- Enabled : & enabled ,
155- OnlyProtectedBranches : & onlyProtectedBranches ,
156- KeepDivergentRefs : & keepDivergentRefs ,
157- }
158- log .Printf ("[DEBUG] Disable gitlab project mirror %v for %s" , mirrorID , projectID )
159-
160- _ , _ , err := client .ProjectMirrors .EditProjectMirror (projectID , mirrorID , & options , gitlab .WithContext (ctx ))
152+ isDeleteSupported , err := isGitLabVersionAtLeast (ctx , client , "14.10" )()
161153 if err != nil {
162154 return diag .FromErr (err )
163155 }
164156
157+ if isDeleteSupported {
158+ log .Printf ("[DEBUG] delete gitlab project mirror %v for %s" , mirrorID , projectID )
159+
160+ _ , err := client .ProjectMirrors .DeleteProjectMirror (projectID , mirrorID , gitlab .WithContext (ctx ))
161+ if err != nil {
162+ return diag .FromErr (err )
163+ }
164+ } else {
165+ // NOTE: this code only exists to support GitLab < 14.10.
166+ // It can be removed once ~ GitLab 15.2 is out and supported.
167+ options := gitlab.EditProjectMirrorOptions {Enabled : gitlab .Bool (false )}
168+ log .Printf ("[DEBUG] Disable gitlab project mirror %v for %s" , mirrorID , projectID )
169+ _ , _ , err := client .ProjectMirrors .EditProjectMirror (projectID , mirrorID , & options , gitlab .WithContext (ctx ))
170+ if err != nil {
171+ return diag .FromErr (err )
172+ }
173+ }
174+
165175 return nil
166176}
167177
@@ -170,42 +180,19 @@ func resourceGitlabProjectMirrorRead(ctx context.Context, d *schema.ResourceData
170180
171181 ids := strings .Split (d .Id (), ":" )
172182 projectID := ids [0 ]
173- mirrorID := ids [1 ]
174- integerMirrorID , err := strconv .Atoi (mirrorID )
183+ rawMirrorID := ids [1 ]
184+ mirrorID , err := strconv .Atoi (rawMirrorID )
175185 if err != nil {
176186 return diag .FromErr (err )
177187 }
178188 log .Printf ("[DEBUG] read gitlab project mirror %s id %v" , projectID , mirrorID )
179-
180- var mirror * gitlab.ProjectMirror
181- found := false
182-
183- opts := & gitlab.ListProjectMirrorOptions {
184- Page : 1 ,
185- PerPage : 20 ,
186- }
187-
188- for {
189- mirrors , response , err := client .ProjectMirrors .ListProjectMirror (projectID , opts , gitlab .WithContext (ctx ))
190- if err != nil {
191- return diag .FromErr (err )
192- }
193-
194- for _ , m := range mirrors {
195- log .Printf ("[DEBUG] project mirror found %v" , m .ID )
196- if m .ID == integerMirrorID {
197- mirror = m
198- found = true
199- break
200- }
201- }
202- if response .CurrentPage >= response .TotalPages {
203- break
204- }
205- opts .Page ++
189+ mirror , err := resourceGitLabProjectMirrorGetMirror (ctx , client , projectID , mirrorID )
190+ if err != nil {
191+ return diag .FromErr (err )
206192 }
207193
208- if ! found {
194+ if mirror == nil {
195+ log .Printf ("[DEBUG] mirror %d in project %s not found, removing from state" , mirrorID , projectID )
209196 d .SetId ("" )
210197 return nil
211198 }
@@ -222,3 +209,47 @@ func resourceGitlabProjectMirrorSetToState(d *schema.ResourceData, projectMirror
222209 d .Set ("project" , projectID )
223210 d .Set ("url" , projectMirror .URL )
224211}
212+
213+ func resourceGitLabProjectMirrorGetMirror (ctx context.Context , client * gitlab.Client , projectID string , mirrorID int ) (* gitlab.ProjectMirror , error ) {
214+ isGetProjectMirrorSupported , err := isGitLabVersionAtLeast (ctx , client , "14.10" )()
215+ if err != nil {
216+ return nil , err
217+ }
218+
219+ var mirror * gitlab.ProjectMirror
220+
221+ if isGetProjectMirrorSupported {
222+ mirror , _ , err = client .ProjectMirrors .GetProjectMirror (projectID , mirrorID , gitlab .WithContext (ctx ))
223+ if err != nil {
224+ if is404 (err ) {
225+ return nil , nil
226+ }
227+ return nil , err
228+ }
229+ } else {
230+ // NOTE: remove this branch and move logic back to Read() function when GitLab older than 14.10 are not longer supported by this provider
231+ found := false
232+ options := & gitlab.ListProjectMirrorOptions {
233+ Page : 1 ,
234+ PerPage : 20 ,
235+ }
236+
237+ for options .Page != 0 && ! found {
238+ mirrors , resp , err := client .ProjectMirrors .ListProjectMirror (projectID , options , gitlab .WithContext (ctx ))
239+ if err != nil {
240+ return nil , err
241+ }
242+
243+ for _ , m := range mirrors {
244+ if m .ID == mirrorID {
245+ mirror = m
246+ found = true
247+ break
248+ }
249+ }
250+ options .Page = resp .NextPage
251+ }
252+ }
253+
254+ return mirror , nil
255+ }
0 commit comments