github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/api/controller/instancepoller/machine.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package instancepoller
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names/v5"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	"github.com/juju/juju/api/common"
    12  	"github.com/juju/juju/core/instance"
    13  	"github.com/juju/juju/core/life"
    14  	"github.com/juju/juju/core/network"
    15  	"github.com/juju/juju/core/status"
    16  	"github.com/juju/juju/rpc/params"
    17  )
    18  
    19  // Machine represents a juju machine as seen by an instancepoller
    20  // worker.
    21  type Machine struct {
    22  	facade base.FacadeCaller
    23  
    24  	tag  names.MachineTag
    25  	life life.Value
    26  }
    27  
    28  // Id returns the machine's id.
    29  func (m *Machine) Id() string {
    30  	return m.tag.Id()
    31  }
    32  
    33  // Tag returns the machine's tag.
    34  func (m *Machine) Tag() names.MachineTag {
    35  	return m.tag
    36  }
    37  
    38  // String returns the machine as a string.
    39  func (m *Machine) String() string {
    40  	return m.Id()
    41  }
    42  
    43  // Life returns the machine's lifecycle value.
    44  func (m *Machine) Life() life.Value {
    45  	return m.life
    46  }
    47  
    48  // Refresh updates the cached local copy of the machine's data.
    49  func (m *Machine) Refresh() error {
    50  	life, err := common.OneLife(m.facade, m.tag)
    51  	if err != nil {
    52  		return errors.Trace(err)
    53  	}
    54  	m.life = life
    55  	return nil
    56  }
    57  
    58  // Status returns the machine status.
    59  func (m *Machine) Status() (params.StatusResult, error) {
    60  	var results params.StatusResults
    61  	args := params.Entities{Entities: []params.Entity{
    62  		{Tag: m.tag.String()},
    63  	}}
    64  	err := m.facade.FacadeCall("Status", args, &results)
    65  	if err != nil {
    66  		return params.StatusResult{}, errors.Trace(err)
    67  	}
    68  	if len(results.Results) != 1 {
    69  		err := errors.Errorf("expected 1 result, got %d", len(results.Results))
    70  		return params.StatusResult{}, err
    71  	}
    72  	result := results.Results[0]
    73  	if result.Error != nil {
    74  		return params.StatusResult{}, result.Error
    75  	}
    76  	return result, nil
    77  }
    78  
    79  // IsManual returns whether the machine is manually provisioned.
    80  func (m *Machine) IsManual() (bool, error) {
    81  	var results params.BoolResults
    82  	args := params.Entities{Entities: []params.Entity{
    83  		{Tag: m.tag.String()},
    84  	}}
    85  	err := m.facade.FacadeCall("AreManuallyProvisioned", args, &results)
    86  	if err != nil {
    87  		return false, errors.Trace(err)
    88  	}
    89  	if len(results.Results) != 1 {
    90  		err := errors.Errorf("expected 1 result, got %d", len(results.Results))
    91  		return false, err
    92  	}
    93  	result := results.Results[0]
    94  	if result.Error != nil {
    95  		return false, result.Error
    96  	}
    97  	return result.Result, nil
    98  }
    99  
   100  // InstanceId returns the machine's instance id.
   101  func (m *Machine) InstanceId() (instance.Id, error) {
   102  	var results params.StringResults
   103  	args := params.Entities{Entities: []params.Entity{
   104  		{Tag: m.tag.String()},
   105  	}}
   106  	err := m.facade.FacadeCall("InstanceId", args, &results)
   107  	if err != nil {
   108  		return "", errors.Trace(err)
   109  	}
   110  	if len(results.Results) != 1 {
   111  		err := errors.Errorf("expected 1 result, got %d", len(results.Results))
   112  		return "", err
   113  	}
   114  	result := results.Results[0]
   115  	if result.Error != nil {
   116  		return "", result.Error
   117  	}
   118  	return instance.Id(result.Result), nil
   119  }
   120  
   121  // InstanceStatus returns the machine's instance status.
   122  func (m *Machine) InstanceStatus() (params.StatusResult, error) {
   123  	var results params.StatusResults
   124  	args := params.Entities{Entities: []params.Entity{
   125  		{Tag: m.tag.String()},
   126  	}}
   127  	err := m.facade.FacadeCall("InstanceStatus", args, &results)
   128  	if err != nil {
   129  		return params.StatusResult{}, errors.Trace(err)
   130  	}
   131  	if len(results.Results) != 1 {
   132  		err := errors.Errorf("expected 1 result, got %d", len(results.Results))
   133  		return params.StatusResult{}, err
   134  	}
   135  	result := results.Results[0]
   136  	if result.Error != nil {
   137  		return params.StatusResult{}, result.Error
   138  	}
   139  	return result, nil
   140  }
   141  
   142  // SetInstanceStatus sets the instance status of the machine.
   143  func (m *Machine) SetInstanceStatus(status status.Status, message string, data map[string]interface{}) error {
   144  	var result params.ErrorResults
   145  	args := params.SetStatus{Entities: []params.EntityStatusArgs{
   146  		{Tag: m.tag.String(), Status: status.String(), Info: message, Data: data},
   147  	}}
   148  	err := m.facade.FacadeCall("SetInstanceStatus", args, &result)
   149  	if err != nil {
   150  		return err
   151  	}
   152  	return result.OneError()
   153  }
   154  
   155  // SetProviderNetworkConfig updates the provider addresses for this machine.
   156  func (m *Machine) SetProviderNetworkConfig(ifList network.InterfaceInfos) (network.ProviderAddresses, bool, error) {
   157  	var results params.SetProviderNetworkConfigResults
   158  	args := params.SetProviderNetworkConfig{
   159  		Args: []params.ProviderNetworkConfig{{
   160  			Tag:     m.tag.String(),
   161  			Configs: params.NetworkConfigFromInterfaceInfo(ifList),
   162  		}},
   163  	}
   164  
   165  	err := m.facade.FacadeCall("SetProviderNetworkConfig", args, &results)
   166  	if err != nil {
   167  		return nil, false, err
   168  	}
   169  
   170  	if len(results.Results) != 1 {
   171  		err := errors.Errorf("expected 1 result, got %d", len(results.Results))
   172  		return nil, false, err
   173  	}
   174  	result := results.Results[0]
   175  	if result.Error != nil {
   176  		return nil, false, result.Error
   177  	}
   178  
   179  	return params.ToProviderAddresses(result.Addresses...), result.Modified, nil
   180  }