Machine creates a record for the associated mac if needed and asked for, and returns a Machine with the stored values. If createIfNeeded is true, and there is no machine associated to this mac, the machine will be created, stored, and returned. In this case, if createWithIP is empty, the IP will be
(createIfNeeded bool, createWithIP net.IP)
| 47 | // Note: if the machine exists, createWithIP is ignored. It's possible |
| 48 | // for the returned Machine to have an IP different from createWithIP. |
| 49 | func (m *etcdMachineInterface) Machine(createIfNeeded bool, |
| 50 | createWithIP net.IP) (Machine, error) { |
| 51 | var machine Machine |
| 52 | |
| 53 | if !createIfNeeded && (createWithIP != nil) { |
| 54 | return machine, errors.New( |
| 55 | "if createIfNeeded is false, the createWithIP is expected to be nil") |
| 56 | } |
| 57 | |
| 58 | resp, err := m.selfGet("_machine") |
| 59 | if err != nil { |
| 60 | errorIsKeyNotFound := etcd.IsKeyNotFound(err) |
| 61 | |
| 62 | if !(errorIsKeyNotFound && createIfNeeded) { |
| 63 | return machine, fmt.Errorf("error while retrieving _machine: %s", err) |
| 64 | } |
| 65 | |
| 66 | machine := Machine{ |
| 67 | IP: createWithIP, // to be assigned automatically |
| 68 | FirstSeen: time.Now().Unix(), |
| 69 | } |
| 70 | err := m.store(&machine) |
| 71 | if err != nil { |
| 72 | return machine, fmt.Errorf("error while storing _machine: %s", err) |
| 73 | } |
| 74 | return machine, nil |
| 75 | } |
| 76 | json.Unmarshal([]byte(resp), &machine) |
| 77 | return machine, nil |
| 78 | } |
| 79 | |
| 80 | func (m *etcdMachineInterface) store(machine *Machine) error { |
| 81 | if machine.Type == 0 { |