// AddSubnetsWithTemplate adds numSubnets subnets, using the given // infoTemplate. Any string field in the infoTemplate can be specified // as a text/template string containing {{.}}, which is the current // index of the subnet-to-add (between 0 and numSubnets-1). // // Example: // // AddSubnetsWithTemplate(c, st, 2, state.SubnetInfo{ // CIDR: "10.10.{{.}}.0/24", // ProviderId: "subnet-{{.}}", // SpaceName: "space1", // AvailabilityZone: "zone-{{.}}", // AllocatableIPLow: "{{if (gt . 0)}}10.10.{{.}}.5{{end}}", // AllocatableIPHigh: "{{if (gt . 0)}}10.10.{{.}}.254{{end}}", // VLANTag: 42, // }) // // This is equivalent to the following calls: // // _, err := st.AddSubnet(state.SubnetInfo{ // CIDR: "10.10.0.0/24", // ProviderId: "subnet-0", // SpaceName: "space1", // AvailabilityZone: "zone-0", // VLANTag: 42, // }) // c.Assert(err, jc.ErrorIsNil) // _, err = st.AddSubnet(state.SubnetInfo{ // CIDR: "10.10.1.0/24", // ProviderId: "subnet-1", // SpaceName: "space1", // AvailabilityZone: "zone-1", // AllocatableIPLow: "10.10.1.5", // AllocatableIPHigh: "10.10.1.254", // VLANTag: 42, // }) func AddSubnetsWithTemplate(c *gc.C, st *state.State, numSubnets uint, infoTemplate state.SubnetInfo) { for subnetIndex := 0; subnetIndex < int(numSubnets); subnetIndex++ { info := infoTemplate // make a copy each time. // permute replaces the contents of *s with the result of interpreting // *s as a template. permute := func(s string) string { t, err := template.New("").Parse(s) c.Assert(err, jc.ErrorIsNil) var buf bytes.Buffer err = t.Execute(&buf, subnetIndex) c.Assert(err, jc.ErrorIsNil) return buf.String() } info.ProviderId = network.Id(permute(string(info.ProviderId))) info.CIDR = permute(info.CIDR) info.AllocatableIPHigh = permute(info.AllocatableIPHigh) info.AllocatableIPLow = permute(info.AllocatableIPLow) info.AvailabilityZone = permute(info.AvailabilityZone) info.SpaceName = permute(info.SpaceName) _, err := st.AddSubnet(info) c.Assert(err, jc.ErrorIsNil) } }
// AddSubnetsWithTemplate adds numSubnets subnets, using the given // infoTemplate. Any string field in the infoTemplate can be specified // as a text/template string containing {{.}}, which is the current // index of the subnet-to-add (between 0 and numSubnets-1). // // Example: // // AddSubnetsWithTemplate(c, st, 2, state.SubnetInfo{ // CIDR: "10.10.{{.}}.0/24", // ProviderId: "subnet-{{.}}", // SpaceName: "space1", // AvailabilityZone: "zone-{{.}}", // AllocatableIPLow: "{{if (gt . 0)}}10.10.{{.}}.5{{end}}", // AllocatableIPHigh: "{{if (gt . 0)}}10.10.{{.}}.254{{end}}", // VLANTag: 42, // }) // // This is equivalent to the following calls: // // _, err := st.AddSubnet(state.SubnetInfo{ // CIDR: "10.10.0.0/24", // ProviderId: "subnet-0", // SpaceName: "space1", // AvailabilityZone: "zone-0", // VLANTag: 42, // }) // c.Assert(err, jc.ErrorIsNil) // _, err = st.AddSubnet(state.SubnetInfo{ // CIDR: "10.10.1.0/24", // ProviderId: "subnet-1", // SpaceName: "space1", // AvailabilityZone: "zone-1", // AllocatableIPLow: "10.10.1.5", // AllocatableIPHigh: "10.10.1.254", // VLANTag: 42, // }) func AddSubnetsWithTemplate(c *gc.C, st *state.State, numSubnets uint, infoTemplate state.SubnetInfo) { infot := reflect.TypeOf(infoTemplate) for subnetIndex := 0; subnetIndex < int(numSubnets); subnetIndex++ { info := infoTemplate // make a copy each time. for fieldIndex := 0; fieldIndex < infot.NumField(); fieldIndex++ { fieldv := reflect.ValueOf(&info).Elem().Field(fieldIndex) if fieldv.Kind() != reflect.String { // Skip non string fields. continue } text := fmt.Sprint(fieldv.Interface()) t, err := template.New("").Parse(text) c.Assert(err, jc.ErrorIsNil) var buf bytes.Buffer err = t.Execute(&buf, subnetIndex) c.Assert(err, jc.ErrorIsNil) fieldv.SetString(buf.String()) } _, err := st.AddSubnet(info) c.Assert(err, jc.ErrorIsNil) } }
func (s *SubnetSuite) addTwoSubnetsInDifferentModelsAssertSuccessAndReturnBoth(c *gc.C, info1, info2 state.SubnetInfo, otherState *state.State) (*state.Subnet, *state.Subnet) { subnet1, err := otherState.AddSubnet(info1) c.Assert(err, jc.ErrorIsNil) subnet2, err := s.State.AddSubnet(info2) c.Assert(err, jc.ErrorIsNil) return subnet1, subnet2 }
func (s *SpacesSuite) addSubnetsForState(c *gc.C, CIDRs []string, st *state.State) { if len(CIDRs) == 0 { return } for _, info := range s.makeSubnetInfosForCIDRs(c, CIDRs) { _, err := st.AddSubnet(info) c.Assert(err, jc.ErrorIsNil) } }
func (s *SubnetSuite) addTwoSubnetsInDifferentModelsAndAssertSecondFailsWithSuffix(c *gc.C, info1, info2 state.SubnetInfo, otherState *state.State, errorSuffix string) { _, err := otherState.AddSubnet(info1) c.Assert(err, jc.ErrorIsNil) s.assertAddSubnetForInfoFailsWithSuffix(c, info2, errorSuffix) }