github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/provider/gce/instance.go (about) 1 // Copyright 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package gce 5 6 import ( 7 "github.com/juju/errors" 8 9 "github.com/juju/juju/core/instance" 10 corenetwork "github.com/juju/juju/core/network" 11 "github.com/juju/juju/core/network/firewall" 12 "github.com/juju/juju/core/status" 13 "github.com/juju/juju/environs/context" 14 "github.com/juju/juju/environs/instances" 15 "github.com/juju/juju/provider/gce/google" 16 ) 17 18 type environInstance struct { 19 base *google.Instance 20 env *environ 21 } 22 23 var _ instances.Instance = (*environInstance)(nil) 24 25 func newInstance(base *google.Instance, env *environ) *environInstance { 26 return &environInstance{ 27 base: base, 28 env: env, 29 } 30 } 31 32 // Id implements instances.Instance. 33 func (inst *environInstance) Id() instance.Id { 34 return instance.Id(inst.base.ID) 35 } 36 37 // Status implements instances.Instance. 38 func (inst *environInstance) Status(ctx context.ProviderCallContext) instance.Status { 39 instStatus := inst.base.Status() 40 var jujuStatus status.Status 41 switch instStatus { 42 case "PROVISIONING", "STAGING": 43 jujuStatus = status.Provisioning 44 case "RUNNING": 45 jujuStatus = status.Running 46 case "STOPPING", "TERMINATED": 47 jujuStatus = status.Empty 48 default: 49 jujuStatus = status.Empty 50 } 51 return instance.Status{ 52 Status: jujuStatus, 53 Message: instStatus, 54 } 55 } 56 57 // Addresses implements instances.Instance. 58 func (inst *environInstance) Addresses(ctx context.ProviderCallContext) (corenetwork.ProviderAddresses, error) { 59 return inst.base.Addresses(), nil 60 } 61 62 func findInst(id instance.Id, instances []instances.Instance) instances.Instance { 63 for _, inst := range instances { 64 if id == inst.Id() { 65 return inst 66 } 67 } 68 return nil 69 } 70 71 // firewall stuff 72 73 // OpenPorts opens the given ports on the instance, which 74 // should have been started with the given machine id. 75 func (inst *environInstance) OpenPorts(ctx context.ProviderCallContext, machineID string, rules firewall.IngressRules) error { 76 // TODO(ericsnow) Make sure machineId matches inst.Id()? 77 name, err := inst.env.namespace.Hostname(machineID) 78 if err != nil { 79 return errors.Trace(err) 80 } 81 err = inst.env.gce.OpenPorts(name, rules) 82 return google.HandleCredentialError(errors.Trace(err), ctx) 83 } 84 85 // ClosePorts closes the given ports on the instance, which 86 // should have been started with the given machine id. 87 func (inst *environInstance) ClosePorts(ctx context.ProviderCallContext, machineID string, rules firewall.IngressRules) error { 88 name, err := inst.env.namespace.Hostname(machineID) 89 if err != nil { 90 return errors.Trace(err) 91 } 92 err = inst.env.gce.ClosePorts(name, rules) 93 return google.HandleCredentialError(errors.Trace(err), ctx) 94 } 95 96 // IngressRules returns the set of ingress rules applicable to the instance, which 97 // should have been started with the given machine id. 98 // The rules are returned as sorted by SortIngressRules. 99 func (inst *environInstance) IngressRules(ctx context.ProviderCallContext, machineID string) (firewall.IngressRules, error) { 100 name, err := inst.env.namespace.Hostname(machineID) 101 if err != nil { 102 return nil, errors.Trace(err) 103 } 104 ports, err := inst.env.gce.IngressRules(name) 105 return ports, google.HandleCredentialError(errors.Trace(err), ctx) 106 }