Example #1
0
func Pub(needlogin bool, args []string) (err error) {

	//fmt.Println(args)
	//return

	if len(args) < 2 {
		//fmt.Println(ErrMsgArgument)
		pubUsage()
		return errors.New("args len error!")
	}
	pub := ds.PubPara{}
	//var largs []string = args
	var repo, item, tag, argfi, argse string
	f := mflag.NewFlagSet("datahub pub", mflag.ContinueOnError)
	//f.StringVar(&pub.Datapool, []string{"-datapool", "p"}, "", "datapool name")
	f.StringVar(&pub.Accesstype, []string{"-accesstype", "t"}, "private", "dataitem accesstype: private or public")
	f.StringVar(&pub.Comment, []string{"-comment", "m"}, "", "comments")
	//f.StringVar(&pub.Detail, []string{"-detail", "d"}, "", "tag detail ,for example file name")
	f.StringVar(&pub.SupplyStyle, []string{"-supplystyle", "s"}, "batch", "dataitem supplystyle: batch , flow or api")
	f.StringVar(&pub.Ch_itemname, []string{"-chinese", "ch"}, "", "dataitem's Chinese name")
	f.Usage = pubUsage

	if len(args) > 2 {
		if err = f.Parse(args[2:]); err != nil {
			fmt.Println("Error : parse parameter error.", err)
			return err
		}
	}

	//if pub.Ch_itemname == "" {
	//	fmt.Println("DataHub: Dataitem's Chinese name cannot be empty.")
	//	pubUsage()
	//	return
	//}

	if len(args[0]) == 0 || len(args[1]) == 0 {
		fmt.Println(ErrMsgArgument)
		pubUsage()
		return errors.New("need item or tag error!")
	}

	argfi = strings.Trim(args[0], "/")

	//deal arg[0]
	sp := strings.Split(argfi, "/")
	if len(sp) != 2 {
		fmt.Println(ErrMsgArgument)
		pubUsage()
		return errors.New("invalid repo/item")
	}
	repo = sp[0]
	sptag := strings.Split(sp[1], ":")

	l := len(sptag)
	if l == 1 {
		item = sptag[0]
		argse = strings.Trim(args[1], "/")
		se := strings.Split(argse, "://")
		if len(se) == 2 && len(se[1]) > 0 {
			pub.Datapool = se[0]
			pub.ItemDesc = strings.Trim(se[1], "/")
			err = PubItem(repo, item, pub, args)
		} else {
			fmt.Println("DataHub : Please input a valid datapool and path.")
			err = errors.New("Error : Please input a valid datapool and path.")
		}
	} else if l == 2 {
		item = sptag[0]
		tag = sptag[1]
		pub.Detail = args[1]

		if len(args) == 2 || (len(args) == 3 && strings.Contains(args[2], "-")) {
			PubTag(repo, item, tag, pub, args)
		} else {
			if len(strings.Split(args[2], ":")) != 2 || strings.Split(args[2], ":")[0] == "" {
				fmt.Printf("DataHub : Invalid argument.\nSee '%s --help'.\n", f.Name())
				return
			}
			datapool := strings.Split(args[2], ":")[0]
			pub.Datapool = datapool

			if len(strings.Split(strings.Split(args[2], ":")[1], "//")) != 2 || strings.Split(strings.Split(args[2], ":")[1], "//")[1] == "" {
				fmt.Printf("DataHub : Invalid argument.\nSee '%s --help'.\n", f.Name())
				return
			}
			itemDesc := strings.Split(strings.Split(args[2], ":")[1], "//")[1]

			pub.ItemDesc = itemDesc
			PubTag(repo, item, tag, pub, args)
		}

	} else {
		fmt.Printf("DataHub : Invalid argument.\nSee '%s --help'.\n", f.Name())
		return errors.New("Invalid argument.")
	}

	return err

}
Example #2
0
func pubItemHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	log.Println(r.URL.Path, "(pub dataitem)")
	repo := ps.ByName("repo")
	item := ps.ByName("item")

	if CheckLength(w, repo, MaxRepoLength) == false {
		return
	}

	if CheckLength(w, item, MaxItemLength) == false {
		return
	}

	if len(loginAuthStr) == 0 {
		HttpNoData(w, http.StatusUnauthorized, cmd.ErrorUnAuthorization, "Unlogin")
		return
	}

	pub := ds.PubPara{}
	pub.ItemDesc = strings.Trim(pub.ItemDesc, "/")
	if strings.Contains(pub.ItemDesc, "/") == true {
		log.Println("The path of item can't contain '/'.", pub.ItemDesc)
		w.Write([]byte(`{"msg":"The path of item can't contain '/'."}`))
		return
	}
	if CheckLength(w, pub.Comment, MaxCommentLength) == false {
		return
	}

	reqBody, _ := ioutil.ReadAll(r.Body)
	if err := json.Unmarshal(reqBody, &pub); err != nil {
		HttpNoData(w, http.StatusBadRequest, cmd.ErrorUnmarshal, "pub dataitem error while unmarshal reqBody")
		return
	}

	if CheckDataPoolExist(pub.Datapool) == false {
		HttpNoData(w, http.StatusBadRequest, cmd.ErrorUnmarshal,
			fmt.Sprintf("Datapool %s not found", pub.Datapool))
		return
	}

	//ToDO  check item dir exist

	priceplans := []PricePlan{}

	meta, sample, priceplans := GetMetaAndSampleAndPricePlan(pub.Datapool, pub.ItemDesc)
	icpub := ic{AccessType: pub.Accesstype,
		Comment:     pub.Comment,
		Meta:        meta,
		Sample:      sample,
		Ch_itemname: pub.Ch_itemname}

	//{"itemaccesstype":"private","comment":"","meta":"  ","label":{"sys":{"supply_style":"batch"}}}
	isys := Sys{Supplystyle: pub.SupplyStyle}
	icpub.Slabel = Label{Ssys: isys}
	if len(priceplans) > 0 {
		log.Info(priceplans)
		icpub.PricePlans = priceplans
	}

	body, err := json.Marshal(icpub)
	if err != nil {
		s := "pub dataitem error while marshal icpub struct"
		log.Println(s)
		HttpNoData(w, http.StatusBadRequest, cmd.ErrorMarshal, s)
		return
	}
	log.Println(string(body))

	log.Println("daemon: connecting to", DefaultServer+r.URL.Path)
	req, err := http.NewRequest("POST", DefaultServer+r.URL.Path, bytes.NewBuffer(body))

	req.Header.Set("Authorization", loginAuthStr)

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		s := "Pub dataitem service unavailable"
		HttpNoData(w, http.StatusServiceUnavailable, cmd.ErrorServiceUnavailable, s)
		return
	}
	defer resp.Body.Close()
	//Get server result
	rbody, _ := ioutil.ReadAll(resp.Body)
	log.Println(resp.StatusCode, string(rbody))

	if resp.StatusCode == http.StatusOK {
		err := MkdirForDataItem(repo, item, pub.Datapool, pub.ItemDesc)
		if err != nil {
			RollBackItem(repo, item)
			HttpNoData(w, http.StatusBadRequest, cmd.ErrorInsertItem,
				fmt.Sprintf("Mkdir error! %s", err.Error()))
		} else {
			createTime := time.Now().String()
			err = InsertItemToDb(repo, item, pub.Datapool, pub.ItemDesc, createTime)
			if err != nil {
				RollBackItem(repo, item)
				HttpNoData(w, http.StatusBadRequest, cmd.ErrorInsertItem,
					"Insert dataitem to datapool error, please check it immediately!")
			} else {
				HttpNoData(w, http.StatusOK, cmd.ResultOK, "OK")

				g_DaemonRole = PUBLISHER
			}
		}
	} else {

		result := ds.Result{}
		err = json.Unmarshal(rbody, &result)
		if err != nil {
			s := "Pub dataitem error while unmarshal server response"
			log.Println(s, err)
			HttpNoData(w, resp.StatusCode, cmd.ErrorUnmarshal, s)
			return
		}
		log.Println(resp.StatusCode, result.Msg)
		HttpNoData(w, resp.StatusCode, result.Code, result.Msg)
	}

	return

}