Exemplo n.º 1
0
func segmentAdd(cmd *cli.Command, args []string) error {
	if len(args) != 2 {
		return util.UsageError(cmd, "TENANT and SEGMENT name should be provided.")
	}

	tnt := args[0]
	seg := args[1]

	client, err := getRestClient()
	if err != nil {
		return err
	}

	romanaID, err := romana.GetTenantID(client, tnt)
	if err != nil {
		return errors.New("Romana Tenant doesn't exists: " + tnt)
	}
	romanaIDStr := strconv.FormatUint(romanaID, 10)

	tenantURL, err := client.GetServiceUrl("tenant")
	if err != nil {
		return err
	}

	data := tenant.Segment{Name: seg, ExternalID: externalID}
	var result map[string]interface{}
	err = client.Post(tenantURL+"/tenants/"+romanaIDStr+"/segments",
		data, &result)
	if err != nil {
		return err
	}

	_, tFound := result["name"]
	if !tFound {
		var h common.HttpError
		dc := &ms.DecoderConfig{TagName: "json", Result: &h}
		decoder, err := ms.NewDecoder(dc)
		if err != nil {
			return err
		}
		err = decoder.Decode(result)
		if err != nil {
			return err
		}
		if config.GetString("Format") == "json" {
			status, _ := json.MarshalIndent(h, "", "\t")
			fmt.Println(string(status))
			return fmt.Errorf("HTTP Error")
		}
		return fmt.Errorf(h.Error())
	}

	if config.GetString("Format") == "json" {
		segment := tenant.Segment{}
		dc := &ms.DecoderConfig{TagName: "json", Result: &segment}
		decoder, err := ms.NewDecoder(dc)
		if err != nil {
			return err
		}
		err = decoder.Decode(result)
		if err != nil {
			return err
		}
		body, err := json.MarshalIndent(segment, "", "\t")
		if err != nil {
			return err
		}
		fmt.Println(string(body))
	} else {
		fmt.Printf("Tenant Segment (%s) added successfully.\n", seg)
	}
	return nil
}
Exemplo n.º 2
0
// tenantCreate accepts tenant names as arguments for
// creating new tenants for platform being set in
// config file (~/.romana.yaml) or via command line
// flags.
func tenantCreate(cmd *cli.Command, args []string) error {
	if len(args) < 1 {
		return util.UsageError(cmd,
			fmt.Sprintf("expected at-least 1 argument, saw none"))
	}

	client, err := getRestClient()
	if err != nil {
		return err
	}

	tenantURL, err := client.GetServiceUrl("tenant")
	if err != nil {
		return err
	}

	tenants := []tenant.Tenant{}
	for _, tnt := range args {
		var tntUUID string

		// uncomment this once creating tenant for all
		// platforms are ready. Till then tenants needs
		// to be manually created for every platform and
		// then created using romana command line tools.
		// if adaptor.CreateTenant(tnt) != nil {
		// 	return err
		// }
		// adaptor.GetTenantUUID below wouldn't be needed once
		// adaptor.CreateTenant is supported because CreateTenant
		// will create a new tenant for a specific platform
		// instead of polling for an already created one using
		// GetTenantUUID.
		if externalID == "" {
			tntUUID, err = adaptor.GetTenantUUID(tnt)
			if err != nil {
				switch err {
				case util.ErrUnimplementedFeature:
					return util.UsageError(cmd,
						"[tenantname] --externalid <externalid> should be provided.")
				default:
					return err
				}
			}
		} else {
			if len(args) > 1 {
				return util.UsageError(cmd,
					"[tenantname] --externalid <externalid> should be provided.\n"+
						"Multiple tenants can't be created with same external ID.",
				)
			}
			tntUUID = externalID
		}

		data := tenant.Tenant{Name: tnt, ExternalID: tntUUID}
		var result map[string]interface{}
		err = client.Post(tenantURL+"/tenants", data, &result)
		if err != nil {
			return err
		}
		_, tFound := result["name"]
		if tFound {
			var t tenant.Tenant
			dc := &ms.DecoderConfig{TagName: "json", Result: &t}
			decoder, err := ms.NewDecoder(dc)
			if err != nil {
				return err
			}
			err = decoder.Decode(result)
			if err != nil {
				return err
			}
			tenants = append(tenants, t)
		} else {
			var h common.HttpError
			dc := &ms.DecoderConfig{TagName: "json", Result: &h}
			decoder, err := ms.NewDecoder(dc)
			if err != nil {
				return err
			}
			err = decoder.Decode(result)
			if err != nil {
				return err
			}
			if config.GetString("Format") == "json" {
				status, _ := json.MarshalIndent(h, "", "\t")
				fmt.Println(string(status))
				return fmt.Errorf("HTTP Error")
			}
			return fmt.Errorf(h.Error())
		}
	}

	if config.GetString("Format") == "json" {
		body, err := json.MarshalIndent(tenants, "", "\t")
		if err != nil {
			return err
		}
		fmt.Println(string(body))
	} else {
		w := new(tabwriter.Writer)
		w.Init(os.Stdout, 0, 8, 0, '\t', 0)
		fmt.Println("New Tenant(s) Added:")
		fmt.Fprintln(w, "Id\t",
			"Tenant Name\t",
			"External ID\t",
		)
		for _, t := range tenants {
			fmt.Fprintf(w, "%d \t %s \t %s \t", t.ID,
				t.Name, t.ExternalID)
			fmt.Fprintf(w, "\n")
		}
		w.Flush()
	}

	return nil
}