Skip to content

Commit aebd6cf

Browse files
committed
overwrite:false for statelock
1 parent a5b3378 commit aebd6cf

1 file changed

Lines changed: 33 additions & 18 deletions

File tree

src/services/StateService.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,26 +132,35 @@ export class StateService {
132132
throw new TerraformError(409, stateLockRequest);
133133
}
134134

135-
// TODO Catch overwrite exception
136-
stateLock = await this.stateLockModel.model.create({
137-
pk,
138-
sk,
139-
ownerId: identity.ownerId,
140-
owner: identity.owner,
141-
repoId: identity.repoId,
142-
repo: identity.repo,
143-
workspace: identity.workspace,
144-
id: stateLockRequest.ID,
145-
path,
146-
lockedBy,
147-
});
135+
try {
136+
stateLock = await this.stateLockModel.model.create(
137+
{
138+
pk,
139+
sk,
140+
ownerId: identity.ownerId,
141+
owner: identity.owner,
142+
repoId: identity.repoId,
143+
repo: identity.repo,
144+
workspace: identity.workspace,
145+
id: stateLockRequest.ID,
146+
path,
147+
lockedBy,
148+
},
149+
{ overwrite: false },
150+
);
151+
} catch (e) {
152+
if (e instanceof Error) {
153+
throw new TerraformError(409, stateLockRequest);
154+
}
155+
throw e;
156+
}
148157

149158
await this.stateLockRequestModel.model.update({
150159
pk: stateLockRequest.pk,
151160
sk: stateLockRequest.sk,
152161
stateLock: {
153-
pk: stateLock.attrs.pk,
154-
sk: stateLock.attrs.sk,
162+
pk,
163+
sk,
155164
},
156165
identity: {
157166
pk: identity.pk,
@@ -167,12 +176,18 @@ export class StateService {
167176
): Promise<void> => {
168177
const lockedBy = crypto.createHash('sha256').update(identity.token, 'utf8').digest('base64');
169178

179+
const pk = StateLockModel.prefix('pk', identity.ownerId);
180+
const sk = StateLockModel.prefix('sk', `${identity.repoId}_${identity.workspace}`);
181+
const id = stateLockRequest.ID;
182+
183+
console.log('Releasing state lock');
184+
170185
const [stateLocks] = await this.stateLockModel.model
171-
.query(StateLockModel.prefix('pk', identity.ownerId))
186+
.query(pk)
172187
.where('sk')
173-
.beginsWith(StateLockModel.prefix('sk', `${identity.repoId}_${identity.workspace}`))
188+
.beginsWith(sk)
174189
.filter('id')
175-
.eq(stateLockRequest.ID)
190+
.eq(id)
176191
.exec()
177192
.promise();
178193

0 commit comments

Comments
 (0)