// AddMachine: Handles an incoming CatalogMachineAdditionRequest.
764643
func (s *Service) AddMachine(componentsmachineproviderrpcmessagescatalogmachineadditionrequest *ComponentsMachineProviderRpcMessagesCatalogMachineAdditionRequest) *AddMachineCall {
	c := &AddMachineCall{s: s, urlParams_: make(gensupport.URLParams)}
	c.componentsmachineproviderrpcmessagescatalogmachineadditionrequest = componentsmachineproviderrpcmessagescatalogmachineadditionrequest
	return c
}
544591
func (c *Conn) GetMachine(name string) (dbus.ObjectPath, error) {
	return c.getPath("GetMachine", name)
}
364306
func (st *State) addMachineOps(template MachineTemplate) (*machineDoc, []txn.Op, error) {
	template, err := st.effectiveMachineTemplate(template, st.IsController())
	if err != nil {
		return nil, nil, err
	}
	if template.InstanceId == "" {
		volumeAttachments, err := st.machineTemplateVolumeAttachmentParams(template)
		if err != nil {
			return nil, nil, err
		}
		if err := st.precheckInstance(
			template.Series,
			template.Constraints,
			template.Placement,
			volumeAttachments,
		); err != nil {
			return nil, nil, err
		}
	}
	seq, err := sequence(st, "machine")
	if err != nil {
		return nil, nil, err
	}
	mdoc := st.machineDocForTemplate(template, strconv.Itoa(seq))
	prereqOps, machineOp, err := st.insertNewMachineOps(mdoc, template)
	if err != nil {
		return nil, nil, errors.Trace(err)
	}
	prereqOps = append(prereqOps, assertModelActiveOp(st.ModelUUID()))
	prereqOps = append(prereqOps, insertNewContainerRefOp(st, mdoc.Id))
	if template.InstanceId != "" {
		prereqOps = append(prereqOps, txn.Op{
			C:      instanceDataC,
			Id:     mdoc.DocID,
			Assert: txn.DocMissing,
			Insert: &instanceData{
				DocID:          mdoc.DocID,
				MachineId:      mdoc.Id,
				InstanceId:     template.InstanceId,
				ModelUUID:      mdoc.ModelUUID,
				Arch:           template.HardwareCharacteristics.Arch,
				Mem:            template.HardwareCharacteristics.Mem,
				RootDisk:       template.HardwareCharacteristics.RootDisk,
				RootDiskSource: template.HardwareCharacteristics.RootDiskSource,
				CpuCores:       template.HardwareCharacteristics.CpuCores,
				CpuPower:       template.HardwareCharacteristics.CpuPower,
				Tags:           template.HardwareCharacteristics.Tags,
				AvailZone:      template.HardwareCharacteristics.AvailabilityZone,
			},
		})
	}

	return mdoc, append(prereqOps, machineOp), nil
}
970757
func (p *Provider) DeleteMachine() (err error) {
	err = p.Host.Driver.Remove()
	defer p.Client.Close()
	return
}
396713
func (*Service) CreateMachine(c context.Context, req *crimson.CreateMachineRequest) (*crimson.Machine, error) {
	if err := createMachine(c, req.Machine); err != nil {
		return nil, err
	}
	return req.Machine, nil
}
1353194
func (c *Client) AddNIC(machineID, networkID string) (*NIC, error) {
	resp := new(NIC)
	req := request{
		method:         client.POST,
		url:            makeURL(apiMachines, machineID, apiNICs),
		reqValue:       addNICOptions{networkID},
		resp:           resp,
		expectedStatus: http.StatusCreated,
	}
	if _, err := c.sendRequest(req); err != nil {
		return nil, errors.Newf(err, "failed to add NIC to machine %s on network: %s", machineID, networkID)
	}
	return resp, nil
}
263235
func (c *CloudAPI) AddMachineTags(machineID string, tags map[string]string) (map[string]string, error) {
	machine, err := c.GetMachine(machineID)
	if err != nil {
		return nil, err
	}

	for tag, value := range tags {
		if _, present := machine.Tags[tag]; !present {
			machine.Tags[tag] = value
		}
	}

	return machine.Tags, nil
}
733155
func (st *State) AddMachines(templates ...MachineTemplate) (_ []*Machine, err error) {
	defer errors.DeferredAnnotatef(&err, "cannot add a new machine")
	var ms []*Machine
	var ops []txn.Op
	var mdocs []*machineDoc
	for _, template := range templates {
		mdoc, addOps, err := st.addMachineOps(template)
		if err != nil {
			return nil, errors.Trace(err)
		}
		mdocs = append(mdocs, mdoc)
		ms = append(ms, newMachine(st, mdoc))
		ops = append(ops, addOps...)
	}
	ssOps, err := st.maintainControllersOps(mdocs, nil)
	if err != nil {
		return nil, errors.Trace(err)
	}
	ops = append(ops, ssOps...)
	ops = append(ops, assertModelActiveOp(st.ModelUUID()))
	if err := st.db().RunTransaction(ops); err != nil {
		if errors.Cause(err) == txn.ErrAborted {
			if err := checkModelActive(st); err != nil {
				return nil, errors.Trace(err)
			}
		}
		return nil, errors.Trace(err)
	}
	return ms, nil
}
349909
func (n *Netrc) RemoveMachine(name string) {
	for i, machine := range n.machines {
		if machine.Name == name {
			n.machines = append(n.machines[:i], n.machines[i+1:]...)
			// continue removing but start over since the indexes changed
			n.RemoveMachine(name)
			return
		}
	}
}
115734
func (vb *VirtualBox) PopulateMachineInfo(machine *Machine) error {
	if machine.mobref == "" {
		return fmt.Errorf("Machine missing object reference id")
	}
	// get machine id
	req1 := getMachineIDRequest{Mobref: machine.mobref}
	rsp1 := new(getMachineIDResponse)
	err := vb.send(req1, rsp1)
	if err != nil {
		return err
	}
	machine.id = rsp1.Returnval

	// get machine name
	req2 := getMachineNameRequest{Mobref: machine.mobref}
	rsp2 := new(getMachineNameResponse)
	err = vb.send(req2, rsp2)
	if err != nil {
		return err
	}
	machine.name = rsp2.Returnval

	return nil
}
6648
func (c *Client) UpdateMachineMetadata(machineID string, metadata map[string]string) (map[string]interface{}, error) {
	var resp map[string]interface{}
	req := request{
		method:   client.POST,
		url:      makeURL(apiMachines, machineID, apiMetadata),
		reqValue: metadata,
		resp:     &resp,
	}
	if _, err := c.sendRequest(req); err != nil {
		return nil, errors.Newf(err, "failed to update metadata for machine with id %s", machineID)
	}
	return resp, nil
}
946706
func ApplicationMachines(st *State, application string) ([]string, error) {
	machines, err := st.AllMachines()
	if err != nil {
		return nil, err
	}
	applicationName := unitAppName(application)
	var machineIds []string
	for _, machine := range machines {
		principalSet := set.NewStrings()
		for _, principal := range machine.Principals() {
			principalSet.Add(unitAppName(principal))
		}
		if principalSet.Contains(applicationName) {
			machineIds = append(machineIds, machine.Id())
		}
	}
	return machineIds, nil
}
1340968
func NewMachineManagerAPI(
	backend Backend,
	storageAccess storageInterface,
	pool Pool,
	auth facade.Authorizer,
	modelTag names.ModelTag,
	callCtx context.ProviderCallContext,
	resources facade.Resources,
) (*MachineManagerAPI, error) {
	if !auth.AuthClient() {
		return nil, common.ErrPerm
	}
	return &MachineManagerAPI{
		st:            backend,
		storageAccess: storageAccess,
		pool:          pool,
		authorizer:    auth,
		check:         common.NewBlockChecker(backend),
		modelTag:      modelTag,
		callContext:   callCtx,
		resources:     resources,
	}, nil
}
1250663
func (st *State) AddOneMachine(template MachineTemplate) (*Machine, error) {
	ms, err := st.AddMachines(template)
	if err != nil {
		return nil, err
	}
	return ms[0], nil
}
290071
func (s *VirtualMachineService) NewAddNicToVirtualMachineParams(networkid string, virtualmachineid string) *AddNicToVirtualMachineParams {
	p := &AddNicToVirtualMachineParams{}
	p.p = make(map[string]interface{})
	p.p["networkid"] = networkid
	p.p["virtualmachineid"] = virtualmachineid
	return p
}
40994
func updateMachine(c context.Context, m *crimson.Machine, mask *field_mask.FieldMask) (*crimson.Machine, error) {
	if err := validateMachineForUpdate(m, mask); err != nil {
		return nil, err
	}
	stmt := squirrel.Update("machines")
	for _, path := range mask.Paths {
		switch path {
		case "platform":
			stmt = stmt.Set("platform_id", squirrel.Expr("(SELECT id FROM platforms WHERE name = ?)", m.Platform))
		case "rack":
			stmt = stmt.Set("rack_id", squirrel.Expr("(SELECT id FROM racks WHERE name = ?)", m.Rack))
		case "state":
			stmt = stmt.Set("state", m.State)
		case "description":
			stmt = stmt.Set("description", m.Description)
		case "asset_tag":
			stmt = stmt.Set("asset_tag", m.AssetTag)
		case "service_tag":
			stmt = stmt.Set("service_tag", m.ServiceTag)
		case "deployment_ticket":
			stmt = stmt.Set("deployment_ticket", m.DeploymentTicket)
		case "drac_password":
			stmt = stmt.Set("drac_password", m.DracPassword)
		}
	}
	stmt = stmt.Where("name = ?", m.Name)
	query, args, err := stmt.ToSql()
	if err != nil {
		return nil, errors.Annotate(err, "failed to generate statement").Err()
	}

	tx, err := database.Begin(c)
	if err != nil {
		return nil, errors.Annotate(err, "failed to begin transaction").Err()
	}
	defer tx.MaybeRollback(c)

	_, err = tx.ExecContext(c, query, args...)
	if err != nil {
		switch e, ok := err.(*mysql.MySQLError); {
		case !ok:
			// Type assertion failed.
		case e.Number == mysqlerr.ER_BAD_NULL_ERROR && strings.Contains(e.Message, "'platform_id'"):
			// e.g. "Error 1048: Column 'platform_id' cannot be null".
			return nil, status.Errorf(codes.NotFound, "platform %q does not exist", m.Platform)
		case e.Number == mysqlerr.ER_BAD_NULL_ERROR && strings.Contains(e.Message, "'rack_id'"):
			// e.g. "Error 1048: Column 'rack_id' cannot be null".
			return nil, status.Errorf(codes.NotFound, "rack %q does not exist", m.Rack)
		}
		return nil, errors.Annotate(err, "failed to update machine").Err()
	}
	// The number of rows affected cannot distinguish between zero because the machine didn't exist
	// and zero because the row already matched, so skip looking at the number of rows affected.

	machines, err := listMachines(c, tx, &crimson.ListMachinesRequest{
		Names: []string{m.Name},
	})
	switch {
	case err != nil:
		return nil, errors.Annotate(err, "failed to fetch updated machine").Err()
	case len(machines) == 0:
		return nil, status.Errorf(codes.NotFound, "machine %q does not exist", m.Name)
	}

	if err := tx.Commit(); err != nil {
		return nil, errors.Annotate(err, "failed to commit transaction").Err()
	}
	return machines[0], nil
}
1088204
func WmClientMachineSet(xu *xgbutil.XUtil, win xproto.Window,
	client string) error {

	return xprop.ChangeProp(xu, win, 8, "WM_CLIENT_MACHINE", "STRING",
		([]byte)(client))
}