Context("with valid CPI v1 input", func() {
		It("deletes a previously uploaded stemcell from the rackhd server", func() {
			apiServer, err := helpers.GetRackHDHost()
			Expect(err).ToNot(HaveOccurred())

			c := config.Cpi{ApiServer: apiServer}

			var createInput bosh.MethodArguments
			createInput = append(createInput, "../spec_assets/image")

			baseName, err := cpi.CreateStemcell(c, createInput)
			Expect(err).ToNot(HaveOccurred())

			var deleteInput bosh.MethodArguments
			deleteInput = append(deleteInput, baseName)
			err = cpi.DeleteStemcell(c, deleteInput)
			Expect(err).ToNot(HaveOccurred())

			url := fmt.Sprintf("%s/api/common/files/metadata/%s", c.ApiServer, baseName)
			resp, err := http.Get(url)
			Expect(err).ToNot(HaveOccurred())

			defer resp.Body.Close()
			Expect(resp.StatusCode).To(Equal(404))
		})
	})

	Context("with invalid CPI v1 input", func() {
		It("returns an error", func() {
			apiServer, err := helpers.GetRackHDHost()
			Expect(err).ToNot(HaveOccurred())
	Context("with valid CPI v1 input", func() {
		It("deletes a previously uploaded stemcell from the rackhd server", func() {
			apiServerIP := fmt.Sprintf("%s:8080", os.Getenv("RACKHD_API_URI"))
			Expect(apiServerIP).ToNot(BeEmpty())

			config := config.Cpi{ApiServer: apiServerIP}

			var createInput bosh.MethodArguments
			createInput = append(createInput, "../spec_assets/image")

			baseName, err := cpi.CreateStemcell(config, createInput)
			Expect(err).ToNot(HaveOccurred())

			var deleteInput bosh.MethodArguments
			deleteInput = append(deleteInput, baseName)
			err = cpi.DeleteStemcell(config, deleteInput)
			Expect(err).ToNot(HaveOccurred())

			url := fmt.Sprintf("http://%s/api/common/files/metadata/%s", config.ApiServer, baseName)
			resp, err := http.Get(url)
			Expect(err).ToNot(HaveOccurred())

			defer resp.Body.Close()
			Expect(resp.StatusCode).To(Equal(404))
		})
	})

	Context("with invalid CPI v1 input", func() {
		It("returns an error", func() {
			apiServerIP := fmt.Sprintf("%s:8080", os.Getenv("RACKHD_API_URI"))
			Expect(apiServerIP).ToNot(BeEmpty())
Пример #3
0
func main() {
	responseLogBuffer = new(bytes.Buffer)
	multiWriter := io.MultiWriter(os.Stderr, responseLogBuffer)
	logLevel := os.Getenv("RACKHD_CPI_LOG_LEVEL")
	log.SetOutput(multiWriter)

	switch logLevel {
	case "DEBUG":
		log.SetLevel(log.DebugLevel)
	case "INFO":
		log.SetLevel(log.InfoLevel)
	case "ERROR":
		log.SetLevel(log.ErrorLevel)
	case "FATAL":
		log.SetLevel(log.FatalLevel)
	default:
		log.SetLevel(log.DebugLevel)
	}

	configPath := flag.String("configPath", "", "Path to configuration file")
	flag.Parse()

	file, err := os.Open(*configPath)
	defer file.Close()

	if err != nil {
		log.Error(fmt.Sprintf("unable to open configuration file %s", err))
		exitWithDefaultError(err)
	}

	reqBytes, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		exitWithDefaultError(err)
	}

	req := bosh.CpiRequest{}
	err = json.Unmarshal(reqBytes, &req)
	if err != nil {
		exitWithDefaultError(err)
	}

	cpiConfig, err := config.New(file, req)
	if err != nil {
		exitWithDefaultError(err)
	}

	implemented, err := cpi.ImplementsMethod(req.Method)
	if err != nil {
		exitWithDefaultError(err)
	}

	if !implemented {
		exitWithNotImplementedError(fmt.Errorf("Method: %s is not implemented", req.Method))
	}

	switch req.Method {
	case bosh.CREATE_STEMCELL:
		cid, err := cpi.CreateStemcell(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running CreateStemcell: %s", err))
		}
		exitWithResult(cid)
	case bosh.CREATE_VM:
		vmcid, err := cpi.CreateVM(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running CreateVM: %s", err))
		}
		exitWithResult(vmcid)
	case bosh.DELETE_STEMCELL:
		err = cpi.DeleteStemcell(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running DeleteStemcell: %s", err))
		}
		exitWithResult("")
	case bosh.DELETE_VM:
		err = cpi.DeleteVM(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running DeleteVM: %s", err))
		}
		exitWithResult("")
	case bosh.SET_VM_METADATA:
		err := cpi.SetVMMetadata(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running SetVMMetadata: %s", err))
		}
		exitWithResult("")
	case bosh.HAS_VM:
		hasVM, err := cpi.HasVM(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running HasVM: %s", err))
		}
		exitWithResult(hasVM)
	case bosh.CREATE_DISK:
		diskCID, err := cpi.CreateDisk(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running CreateDisk: %s", err))
		}
		exitWithResult(diskCID)
	case bosh.DELETE_DISK:
		err := cpi.DeleteDisk(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running DeleteDisk: %s", err))
		}
		exitWithResult("")
	case bosh.ATTACH_DISK:
		err := cpi.AttachDisk(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running AttachDisk: %s", err))
		}
		exitWithResult("")
	case bosh.DETACH_DISK:
		err := cpi.DetachDisk(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running DetachDisk: %s", err))
		}
		exitWithResult("")
	case bosh.HAS_DISK:
		diskExists, err := cpi.HasDisk(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running HasDisk: %s", err))
		}
		exitWithResult(diskExists)
	case bosh.GET_DISKS:
		diskCIDs, err := cpi.GetDisks(cpiConfig, req.Arguments)
		if err != nil {
			exitWithDefaultError(fmt.Errorf("Error running GetDisks: %s", err))
		}
		exitWithResult(diskCIDs)
	default:
		exitWithDefaultError(fmt.Errorf("Unexpected command: %s dispatched...aborting", req.Method))
	}
}