~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/resources_mongo.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
148
148
        }}, newInsertCharmStoreResourceOps(res)...)
149
149
}
150
150
 
151
 
func newInsertUnitResourceOps(unitID string, stored storedResource) []txn.Op {
 
151
func newInsertUnitResourceOps(unitID string, stored storedResource, progress *int64) []txn.Op {
152
152
        doc := newUnitResourceDoc(unitID, stored)
 
153
        doc.DownloadProgress = progress
153
154
 
154
155
        return []txn.Op{{
155
156
                C:      resourcesC,
159
160
        }}
160
161
}
161
162
 
162
 
func newUpdateUnitResourceOps(unitID string, stored storedResource) []txn.Op {
 
163
func newUpdateUnitResourceOps(unitID string, stored storedResource, progress *int64) []txn.Op {
163
164
        doc := newUnitResourceDoc(unitID, stored)
 
165
        doc.DownloadProgress = progress
164
166
 
165
167
        // TODO(ericsnow) Using "update" doesn't work right...
166
168
        return append([]txn.Op{{
168
170
                Id:     doc.DocID,
169
171
                Assert: txn.DocExists,
170
172
                Remove: true,
171
 
        }}, newInsertUnitResourceOps(unitID, stored)...)
 
173
        }}, newInsertUnitResourceOps(unitID, stored, progress)...)
 
174
}
 
175
 
 
176
func newRemoveResourcesOps(docs []resourceDoc) []txn.Op {
 
177
        // The likelihood of a race is small and the consequences are minor,
 
178
        // so we don't worry about the corner case of missing a doc here.
 
179
        var ops []txn.Op
 
180
        for _, doc := range docs {
 
181
                // We do not bother to assert txn.DocExists since it will be
 
182
                // gone either way, which is the result we're after.
 
183
                ops = append(ops, txn.Op{
 
184
                        C:      resourcesC,
 
185
                        Id:     doc.DocID,
 
186
                        Remove: true,
 
187
                })
 
188
        }
 
189
        return ops
172
190
}
173
191
 
174
192
// newResolvePendingResourceOps generates transaction operations that
189
207
                Assert: txn.DocExists,
190
208
                Remove: true,
191
209
        }}
 
210
 
 
211
        csRes := charmStoreResource{
 
212
                Resource:   newRes.Resource.Resource,
 
213
                id:         newRes.ID,
 
214
                serviceID:  newRes.ServiceID,
 
215
                lastPolled: time.Now().UTC(),
 
216
        }
 
217
 
192
218
        if exists {
193
 
                return append(ops, newUpdateResourceOps(newRes)...)
 
219
                ops = append(ops, newUpdateResourceOps(newRes)...)
 
220
                return append(ops, newUpdateCharmStoreResourceOps(csRes)...)
194
221
        } else {
195
 
                return append(ops, newInsertResourceOps(newRes)...)
 
222
                ops = append(ops, newInsertResourceOps(newRes)...)
 
223
                return append(ops, newInsertCharmStoreResourceOps(csRes)...)
196
224
        }
197
225
}
198
226
 
236
264
        return docs, nil
237
265
}
238
266
 
 
267
func (p ResourcePersistence) unitResources(unitID string) ([]resourceDoc, error) {
 
268
        var docs []resourceDoc
 
269
        query := bson.D{{"unit-id", unitID}}
 
270
        if err := p.base.All(resourcesC, query, &docs); err != nil {
 
271
                return nil, errors.Trace(err)
 
272
        }
 
273
        return docs, nil
 
274
}
 
275
 
239
276
// getOne returns the resource that matches the provided model ID.
240
277
func (p ResourcePersistence) getOne(resID string) (resourceDoc, error) {
241
278
        logger.Tracef("querying db for resource %q", resID)
283
320
 
284
321
        StoragePath string `bson:"storage-path"`
285
322
 
 
323
        DownloadProgress *int64 `bson:"download-progress,omitempty"`
 
324
 
286
325
        LastPolled time.Time `bson:"timestamp-when-last-polled"`
287
326
}
288
327