Merge pull request #31223 from Kagashino/fix/cos-backend-unlock-tag

Backend/COS: remove COS object lock tag either when unlock remote state
This commit is contained in:
James Bardin 2022-06-14 12:21:01 -04:00 committed by GitHub
commit e7e3d80850
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
}
@ -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
@ -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()