Skip to content

Commit

Permalink
Rename machinePortRanges to deployedPortRanges
Browse files Browse the repository at this point in the history
  • Loading branch information
ycliuhw committed Feb 27, 2023
1 parent a63bb2d commit 185328f
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
12 changes: 5 additions & 7 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type application struct {
Tools_ *agentTools `yaml:"tools,omitempty"`
OperatorStatus_ *status `yaml:"operator-status,omitempty"`

OpenedPortRanges_ *machinePortRanges `yaml:"opened-port-ranges,omitempty"`
OpenedPortRanges_ *deployedPortRanges `yaml:"opened-port-ranges,omitempty"`

// Offer-related fields
Offers_ *applicationOffers `yaml:"offers,omitempty"`
Expand Down Expand Up @@ -351,20 +351,18 @@ func (a *application) MetricsCredentials() []byte {
return creds
}

// OpenedPortRanges implements Machine.
// OpenedPortRanges implements Application.
func (a *application) OpenedPortRanges() PortRanges {
if a.OpenedPortRanges_ == nil {
// machinePortRanges is not a good name here anymore.
// But we decide to reuse it for application port ranges because the struct format has not changed.
a.OpenedPortRanges_ = newMachinePortRanges()
a.OpenedPortRanges_ = newDeployedPortRanges()
}
return a.OpenedPortRanges_
}

// AddOpenedPortRange implements Machine.
// AddOpenedPortRange implements Application.
func (a *application) AddOpenedPortRange(args OpenedPortRangeArgs) {
if a.OpenedPortRanges_ == nil {
a.OpenedPortRanges_ = newMachinePortRanges()
a.OpenedPortRanges_ = newDeployedPortRanges()
}

if a.OpenedPortRanges_.ByUnit_[args.UnitName] == nil {
Expand Down
6 changes: 3 additions & 3 deletions machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type machine struct {

Containers_ []*machine `yaml:"containers"`

OpenedPortRanges_ *machinePortRanges `yaml:"opened-port-ranges,omitempty"`
OpenedPortRanges_ *deployedPortRanges `yaml:"opened-port-ranges,omitempty"`

Annotations_ `yaml:"annotations,omitempty"`

Expand Down Expand Up @@ -333,15 +333,15 @@ func (m *machine) AddContainer(args MachineArgs) Machine {
// OpenedPortRanges implements Machine.
func (m *machine) OpenedPortRanges() PortRanges {
if m.OpenedPortRanges_ == nil {
m.OpenedPortRanges_ = newMachinePortRanges()
m.OpenedPortRanges_ = newDeployedPortRanges()
}
return m.OpenedPortRanges_
}

// AddOpenedPortRange implements Machine.
func (m *machine) AddOpenedPortRange(args OpenedPortRangeArgs) {
if m.OpenedPortRanges_ == nil {
m.OpenedPortRanges_ = newMachinePortRanges()
m.OpenedPortRanges_ = newDeployedPortRanges()
}

if m.OpenedPortRanges_.ByUnit_[args.UnitName] == nil {
Expand Down
18 changes: 9 additions & 9 deletions port_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ type OpenedPortRangeArgs struct {
Protocol string
}

type machinePortRanges struct {
type deployedPortRanges struct {
Version int `yaml:"version"`

// The set of opened port ranges by unit.
ByUnit_ map[string]*unitPortRanges `yaml:"machine-port-ranges"`
}

func newMachinePortRanges() *machinePortRanges {
return &machinePortRanges{
func newDeployedPortRanges() *deployedPortRanges {
return &deployedPortRanges{
Version: 1,
ByUnit_: make(map[string]*unitPortRanges),
}
}

// ByUnit implements MachinePortRanges.
func (p *machinePortRanges) ByUnit() map[string]UnitPortRanges {
// ByUnit implements deployedPortRanges.
func (p *deployedPortRanges) ByUnit() map[string]UnitPortRanges {
res := make(map[string]UnitPortRanges, len(p.ByUnit_))
for unitName, upr := range p.ByUnit_ {
res[unitName] = upr
Expand Down Expand Up @@ -113,7 +113,7 @@ func (p unitPortRange) Protocol() string {
return p.Protocol_
}

func importMachinePortRanges(source map[string]interface{}) (*machinePortRanges, error) {
func importMachinePortRanges(source map[string]interface{}) (*deployedPortRanges, error) {
checker := versionedMapChecker("machine-port-ranges")
coerced, err := checker.Coerce(source, nil)
if err != nil {
Expand All @@ -130,13 +130,13 @@ func importMachinePortRanges(source map[string]interface{}) (*machinePortRanges,
return importFunc(sourceMap)
}

type machinePortRangeDeserializationFunc func(map[string]interface{}) (*machinePortRanges, error)
type machinePortRangeDeserializationFunc func(map[string]interface{}) (*deployedPortRanges, error)

var machinePortRangeDeserializationFuncs = map[int]machinePortRangeDeserializationFunc{
1: importMachinePortRangeV1,
}

func importMachinePortRangeV1(source map[string]interface{}) (*machinePortRanges, error) {
func importMachinePortRangeV1(source map[string]interface{}) (*deployedPortRanges, error) {
unitChecker := schema.FieldMap(schema.Fields{
"unit-port-ranges": schema.StringMap(
schema.List(schema.StringMap(schema.Any())),
Expand All @@ -149,7 +149,7 @@ func importMachinePortRangeV1(source map[string]interface{}) (*machinePortRanges
"protocol": schema.String(),
}, nil) // no defaults

mpr := &machinePortRanges{
mpr := &deployedPortRanges{
Version: 1,
ByUnit_: make(map[string]*unitPortRanges),
}
Expand Down
2 changes: 1 addition & 1 deletion port_ranges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func assertUnitPortRangeMatches(c *gc.C, prA, prB UnitPortRange) {
var _ = gc.Suite(&MachinePortRangeSerializationSuite{})

func (*MachinePortRangeSerializationSuite) TestParsingSerializedData(c *gc.C) {
initial := &machinePortRanges{
initial := &deployedPortRanges{
Version: 1,
ByUnit_: map[string]*unitPortRanges{
"lorem/0": {
Expand Down
2 changes: 1 addition & 1 deletion ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// particular subnet. OpenedPorts are always associated with a Machine.
//
// This type is deprecated and retained for backwards-compatibility purposes.
// The MachinePortRanges interface should be used instead.
// The PortRange interface should be used instead.
type OpenedPorts interface {
SubnetID() string
OpenPorts() []PortRange
Expand Down

0 comments on commit 185328f

Please sign in to comment.