Skip to content

Commit

Permalink
Merge pull request #31223 from Kagashino/fix/cos-backend-unlock-tag
Browse files Browse the repository at this point in the history
Backend/COS: remove COS object lock tag either when unlock remote state
  • Loading branch information
jbardin committed Jun 14, 2022
2 parents cbf86e0 + cab6cee commit e7e3d80
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions internal/backend/remote-state/cos/client.go
Expand Up @@ -123,6 +123,11 @@ func (c *remoteClient) Unlock(check string) error {
return c.lockError(err)
}

err = c.cosUnlock(c.bucket, c.lockFile)
if err != nil {
return c.lockError(err)
}

return nil
}

Expand Down Expand Up @@ -362,6 +367,16 @@ func (c *remoteClient) cosUnlock(bucket, cosFile string) error {

var err error
for i := 0; i < 30; i++ {
tagExists, err := c.CheckTag(lockTagKey, lockTagValue)

if err != nil {
return err
}

if !tagExists {
return nil
}

err = c.DeleteTag(lockTagKey, lockTagValue)
if err == nil {
return nil
Expand All @@ -372,6 +387,30 @@ func (c *remoteClient) cosUnlock(bucket, cosFile string) error {
return err
}

// CheckTag checks if tag key:value exists
func (c *remoteClient) CheckTag(key, value string) (exists bool, err error) {
request := tag.NewDescribeTagsRequest()
request.TagKey = &key
request.TagValue = &value

response, err := c.tagClient.DescribeTags(request)
log.Printf("[DEBUG] create tag %s:%s: error: %v", key, value, err)
if err != nil {
return
}

if len(response.Response.Tags) == 0 {
return
}

tagKey := response.Response.Tags[0].TagKey
tagValue := response.Response.Tags[0].TagValue

exists = key == *tagKey && value == *tagValue

return
}

// CreateTag create tag by key and value
func (c *remoteClient) CreateTag(key, value string) error {
request := tag.NewCreateTagRequest()
Expand Down

0 comments on commit e7e3d80

Please sign in to comment.