UpdateDevice updates device info in metadata store. The callback should be used to indicate whether device info update was successful or not. An error returned from the callback will rollback the update transaction in the database. Name and Device ID are not allowed to change.
(ctx context.Context, name string, fn DeviceInfoCallback)
| 225 | // An error returned from the callback will rollback the update transaction in the database. |
| 226 | // Name and Device ID are not allowed to change. |
| 227 | func (m *PoolMetadata) UpdateDevice(ctx context.Context, name string, fn DeviceInfoCallback) error { |
| 228 | return m.db.Update(func(tx *bolt.Tx) error { |
| 229 | var ( |
| 230 | device = &DeviceInfo{} |
| 231 | bucket = tx.Bucket(devicesBucketName) |
| 232 | ) |
| 233 | |
| 234 | if err := getObject(bucket, name, device); err != nil { |
| 235 | return err |
| 236 | } |
| 237 | |
| 238 | // Don't allow changing these values, keep things in sync with devmapper |
| 239 | name := device.Name |
| 240 | devID := device.DeviceID |
| 241 | |
| 242 | if err := fn(device); err != nil { |
| 243 | return err |
| 244 | } |
| 245 | |
| 246 | if name != device.Name { |
| 247 | return fmt.Errorf("failed to update device info, name didn't match: %q %q", name, device.Name) |
| 248 | } |
| 249 | |
| 250 | if devID != device.DeviceID { |
| 251 | return fmt.Errorf("failed to update device info, device id didn't match: %d %d", devID, device.DeviceID) |
| 252 | } |
| 253 | |
| 254 | return putObject(bucket, name, device, true) |
| 255 | }) |
| 256 | } |
| 257 | |
| 258 | // GetDevice retrieves device info by name from database |
| 259 | func (m *PoolMetadata) GetDevice(ctx context.Context, name string) (*DeviceInfo, error) { |