示例#1
0
func (manifest Manifest) SetJobInstanceCount(jobName string, count int) (Manifest, error) {
	job, err := findJob(manifest, jobName)
	if err != nil {
		return Manifest{}, err
	}

	job.Instances = count

	if len(job.Networks) == 0 {
		return Manifest{}, fmt.Errorf("%q job must have an existing network to modify", jobName)
	}
	network := job.Networks[0]

	if count == 0 {
		job.Networks[0].StaticIPs = []string{}
		return manifest, nil
	}

	if len(network.StaticIPs) == 0 {
		return Manifest{}, fmt.Errorf("%q job must have at least one static ip in its network", jobName)
	}
	firstIP, err := core.ParseIP(network.StaticIPs[0])
	if err != nil {
		return Manifest{}, err
	}
	lastIP := firstIP.Add(count - 1)
	job.Networks[0].StaticIPs = firstIP.To(lastIP)

	return manifest, nil
}
示例#2
0
func (manifest ManifestV2) SetInstanceCount(instance string, count int) (ManifestV2, error) {
	instanceGroup, err := manifest.GetInstanceGroup(instance)
	if err != nil {
		return ManifestV2{}, err
	}
	instanceGroup.Instances = count
	if count == 0 {
		instanceGroup.Networks[0].StaticIPs = []string{}
		return manifest, nil
	}

	network := instanceGroup.Networks[0]
	firstIP, err := core.ParseIP(network.StaticIPs[0])
	if err != nil {
		return ManifestV2{}, err
	}
	lastIP := firstIP.Add(count - 1)
	instanceGroup.Networks[0].StaticIPs = firstIP.To(lastIP)

	return manifest, nil
}
package core_test

import (
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pivotal-cf-experimental/destiny/core"
)

var _ = Describe("IP", func() {
	Describe("ParseIP", func() {
		It("returns an IP object that represents IP from string", func() {
			ip, err := core.ParseIP("10.0.16.255")
			Expect(err).NotTo(HaveOccurred())
			Expect(ip.String()).To(Equal("10.0.16.255"))
		})

		Context("failure cases", func() {
			It("returns an error if it cannot parse ip", func() {
				_, err := core.ParseIP("not valid")
				Expect(err).To(MatchError(ContainSubstring("not a valid ip address")))
			})

			It("returns an error if ip parts are not digits", func() {
				_, err := core.ParseIP("x.x.x.x")
				Expect(err).To(MatchError(ContainSubstring("invalid syntax")))
			})

			It("returns an error if ip parts are out of the allowed range", func() {
				_, err := core.ParseIP("999.999.999.999")
				Expect(err).To(MatchError(ContainSubstring("values out of range")))
			})