Skip to content

Commit

Permalink
Make changes backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Naduni Pamudika committed Mar 8, 2025
1 parent 402632c commit 17918a3
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 36 deletions.
2 changes: 1 addition & 1 deletion import-export-cli/cmd/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const AiCmdLongDesc = `Perform AI related operations such as uploading APIs and
const AiCmdExamples = utils.ProjectName + ` ` + AiCmdLiteral + ` ` + UploadCmdLiteral + ` ` + UploadAPIsCmdLiteral + ` -e production
` + utils.ProjectName + ` ` + AiCmdLiteral + ` ` + UploadCmdLiteral + ` ` + UploadAPIProductsCmdLiteral + ` -e production
` + utils.ProjectName + ` ` + AiCmdLiteral + ` ` + UploadCmdLiteral + ` ` + UploadAPIsCmdLiteral + ` -e production --all
` + utils.ProjectName + ` ` + AiCmdLiteral + ` ` + PurgeCmdLiteral + ` ` + UploadAPIsCmdLiteral + ` -e production --all
` + utils.ProjectName + ` ` + AiCmdLiteral + ` ` + PurgeCmdLiteral + ` ` + PurgeAPIsCmdLiteral + ` -e production --all
NOTE:The flag (--environment (-e)) is mandatory`

// AiCmd represents the Ai command
Expand Down
25 changes: 18 additions & 7 deletions import-export-cli/cmd/aiDeleteArtifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ const PurgeAPIsCmdLongDesc = `Purge APIs and API Products of a tenant from one e
const purgeAPIsCmdExamples = utils.ProjectName + ` ` + AiCmdLiteral + ` ` + PurgeCmdLiteral + ` ` + PurgeAPIsCmdLiteral + ` -e production
NOTE:The flag (--environment (-e)) is mandatory`

var (
token string
)

var PurgeAPIsCmd = &cobra.Command{
Use: PurgeAPIsCmdLiteral + " (--environment <environment-from-which-artifacts-should-be-purged>)",
Use: PurgeAPIsCmdLiteral + " (--endpoint <endpoint-url> --token <on-prem-key-of-the-organization> --environment " +
"<environment-from-which-artifacts-should-be-purged>)",
Short: purgeAPIsCmdShortDesc,
Long: purgeAPIsCmdLongDesc,
Example: purgeAPIsCmdExamples,
Expand All @@ -51,28 +56,34 @@ var PurgeAPIsCmd = &cobra.Command{
if err != nil {
utils.HandleErrorAndExit("Error getting AI key", err)
}
token, err := impl.GetAIToken(key, CmdPurgeEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting AI token", err)
if (oldToken != "") {
token = oldToken
} else {
token, err = impl.GetAIToken(key, CmdPurgeEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting AI token", err)
}
}
executeAIDeleteAPIsCmd(cred, token)
executeAIDeleteAPIsCmd(cred, token, oldEndpoint)
},
}

// Do operations to Purge APIs to the vector database
func executeAIDeleteAPIsCmd(credential credentials.Credential, token string) {
func executeAIDeleteAPIsCmd(credential credentials.Credential, token, oldEndpoint string) {
var Tenant string
if !strings.Contains(credential.Username, "@") {
Tenant = "carbon.super"
} else {
Tenant = strings.Split(credential.Username, "@")[1]
}
impl.AIDeleteAPIs(credential, CmdPurgeEnvironment, token, Tenant)
impl.AIDeleteAPIs(credential, CmdPurgeEnvironment, token, oldEndpoint, Tenant)
}

func init() {
PurgeCmd.AddCommand(PurgeAPIsCmd)
PurgeAPIsCmd.Flags().StringVarP(&CmdPurgeEnvironment, "environment", "e",
"", "Environment from which the APIs should be Purged")
PurgeAPIsCmd.Flags().StringVarP(&oldToken, "token", "", "", "on-prem-key of the organization")
PurgeAPIsCmd.Flags().StringVarP(&oldEndpoint, "endpoint", "", "", "endpoint of the marketplace assistant service")
_ = PurgeAPIsCmd.MarkFlagRequired("environment")
}
21 changes: 14 additions & 7 deletions import-export-cli/cmd/aiUploadApiProducts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const uploadAPIProductsCmdExamples = utils.ProjectName + ` ` + UploadCmdLiteral
NOTE:The flag (--environment (-e)) is mandatory`

var UploadAPIProductsCmd = &cobra.Command{
Use: UploadAPIProductsCmdLiteral + " (--environment <environment-from-which-artifacts-should-be-uploaded>)",
Use: UploadAPIProductsCmdLiteral + " (--endpoint <endpoint-url> --token <on-prem-key-of-the-organization> --environment " +
"<environment-from-which-artifacts-should-be-uploaded>)",
Short: uploadAPIProductsCmdShortDesc,
Long: uploadAPIProductsCmdLongDesc,
Example: uploadAPIProductsCmdExamples,
Expand All @@ -49,23 +50,29 @@ var UploadAPIProductsCmd = &cobra.Command{
if err != nil {
utils.HandleErrorAndExit("Error getting AI key", err)
}
token, err := impl.GetAIToken(key, CmdUploadEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting AI token", err)
if (oldToken != "") {
token = oldToken
} else {
token, err = impl.GetAIToken(key, CmdUploadEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting AI token", err)
}
}
executeAIUploadAPIProductsCmd(cred, token)
executeAIUploadAPIProductsCmd(cred, token, oldEndpoint)
},
}

// Do operations to upload APIs to the vector database
func executeAIUploadAPIProductsCmd(credential credentials.Credential, token string) {
impl.AIUploadAPIs(credential, CmdUploadEnvironment, token, uploadAll, true)
func executeAIUploadAPIProductsCmd(credential credentials.Credential, token, oldEndpoint string) {
impl.AIUploadAPIs(credential, CmdUploadEnvironment, token, oldEndpoint, uploadAll, true)
}

func init() {
UploadCmd.AddCommand(UploadAPIProductsCmd)
UploadAPIProductsCmd.Flags().StringVarP(&CmdUploadEnvironment, "environment", "e",
"", "Environment from which the APIs should be uploaded")
UploadAPIProductsCmd.Flags().StringVarP(&oldToken, "token", "", "", "on-prem-key of the organization")
UploadAPIProductsCmd.Flags().StringVarP(&oldEndpoint, "endpoint", "", "", "endpoint of the marketplace assistant service")
UploadAPIProductsCmd.Flags().BoolVarP(&uploadAll, "all", "", false,
"Upload both apis and api products")
_ = UploadAPIProductsCmd.MarkFlagRequired("environment")
Expand Down
27 changes: 17 additions & 10 deletions import-export-cli/cmd/aiUploadApis.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ const uploadAPIsCmdExamples = utils.ProjectName + ` ` + UploadCmdLiteral + ` ` +
NOTE:The flag (--environment (-e)) is mandatory`

var (
token string
endpoint string
oldToken string
oldEndpoint string
uploadAll bool
)

var UploadAPIsCmd = &cobra.Command{
Use: UploadAPIsCmdLiteral + " (--environment <environment-from-which-artifacts-should-be-uploaded> --all)",
Use: UploadAPIsCmdLiteral + " (--endpoint <endpoint-url> --token <on-prem-key-of-the-organization> --environment " +
"<environment-from-which-artifacts-should-be-uploaded> --all)",
Short: uploadAPIsCmdShortDesc,
Long: uploadAPIsCmdLongDesc,
Example: uploadAPIsCmdExamples,
Expand All @@ -55,23 +56,29 @@ var UploadAPIsCmd = &cobra.Command{
if err != nil {
utils.HandleErrorAndExit("Error getting AI key", err)
}
token, err := impl.GetAIToken(key, CmdUploadEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting AI token", err)
if (oldToken != "") {
token = oldToken
} else {
token, err = impl.GetAIToken(key, CmdUploadEnvironment)
if err != nil {
utils.HandleErrorAndExit("Error getting AI token", err)
}
}
executeAIUploadAPIsCmd(cred, token)
executeAIUploadAPIsCmd(cred, token, oldEndpoint)
},
}

// Do operatioSns to upload APIs to the vector database
func executeAIUploadAPIsCmd(credential credentials.Credential, token string) {
impl.AIUploadAPIs(credential, CmdUploadEnvironment, token, uploadAll, false)
// Do operations to upload APIs to the vector database
func executeAIUploadAPIsCmd(credential credentials.Credential, token, oldEndpoint string) {
impl.AIUploadAPIs(credential, CmdUploadEnvironment, token, oldEndpoint, uploadAll, false)
}

func init() {
UploadCmd.AddCommand(UploadAPIsCmd)
UploadAPIsCmd.Flags().StringVarP(&CmdUploadEnvironment, "environment", "e",
"", "Environment from which the APIs should be uploaded")
UploadAPIsCmd.Flags().StringVarP(&oldToken, "token", "", "", "on-prem-key of the organization")
UploadAPIsCmd.Flags().StringVarP(&oldEndpoint, "endpoint", "", "", "endpoint of the marketplace assistant service")
UploadAPIsCmd.Flags().BoolVarP(&uploadAll, "all", "", false,
"Upload both apis and api products")
_ = UploadAPIsCmd.MarkFlagRequired("environment")
Expand Down
2 changes: 1 addition & 1 deletion import-export-cli/docs/apictl_ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ apictl ai [flags]
apictl ai upload apis -e production
apictl ai upload api-products -e production
apictl ai upload apis -e production --all
apictl ai delete apis -e production --all
apictl ai delete artifacts -e production --all
NOTE:The flag (--environment (-e)) is mandatory
```

Expand Down
4 changes: 3 additions & 1 deletion import-export-cli/docs/apictl_ai_delete_artifacts.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Purge APIs and API Products of a tenant from one environment from a vector datab
Purge APIs and API Products of a tenant from one environment from a vector database.

```
apictl ai delete artifacts (--environment <environment-from-which-artifacts-should-be-purged>) [flags]
apictl ai delete artifacts (--endpoint <endpoint-url> --token <on-prem-key-of-the-organization> --environment <environment-from-which-artifacts-should-be-purged>) [flags]
```

### Examples
Expand All @@ -20,8 +20,10 @@ NOTE:The flag (--environment (-e)) is mandatory
### Options

```
--endpoint string endpoint of the marketplace assistant service
-e, --environment string Environment from which the APIs should be Purged
-h, --help help for artifacts
--token string on-prem-key of the organization
```

### Options inherited from parent commands
Expand Down
4 changes: 3 additions & 1 deletion import-export-cli/docs/apictl_ai_upload_api-products.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Upload API Products of a tenant from one environment to a vector database.
Upload public API Products of a tenant from one environment specified by flag (--environment, -e)

```
apictl ai upload api-products (--environment <environment-from-which-artifacts-should-be-uploaded>) [flags]
apictl ai upload api-products (--endpoint <endpoint-url> --token <on-prem-key-of-the-organization> --environment <environment-from-which-artifacts-should-be-uploaded>) [flags]
```

### Examples
Expand All @@ -23,8 +23,10 @@ NOTE:The flag (--environment (-e)) is mandatory

```
--all Upload both apis and api products
--endpoint string endpoint of the marketplace assistant service
-e, --environment string Environment from which the APIs should be uploaded
-h, --help help for api-products
--token string on-prem-key of the organization
```

### Options inherited from parent commands
Expand Down
4 changes: 3 additions & 1 deletion import-export-cli/docs/apictl_ai_upload_apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Upload APIs of a tenant from one environment to a vector database.
Upload APIs of a tenant from one environment to a vector database to provide context to the marketplace assistant.

```
apictl ai upload apis (--environment <environment-from-which-artifacts-should-be-uploaded> --all) [flags]
apictl ai upload apis (--endpoint <endpoint-url> --token <on-prem-key-of-the-organization> --environment <environment-from-which-artifacts-should-be-uploaded> --all) [flags]
```

### Examples
Expand All @@ -23,8 +23,10 @@ NOTE:The flag (--environment (-e)) is mandatory

```
--all Upload both apis and api products
--endpoint string endpoint of the marketplace assistant service
-e, --environment string Environment from which the APIs should be uploaded
-h, --help help for apis
--token string on-prem-key of the organization
```

### Options inherited from parent commands
Expand Down
8 changes: 6 additions & 2 deletions import-export-cli/impl/aiDeleteArtifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var (
Endpoint = utils.DefaultAIEndpoint
)

func AIDeleteAPIs(credential credentials.Credential, CmdUploadEnvironment, aiToken, tenant string) {
func AIDeleteAPIs(credential credentials.Credential, CmdPurgeEnvironment, aiToken, oldEndpoint, tenant string) {

headers := make(map[string]string)
if aiToken != "" {
Expand All @@ -50,7 +50,11 @@ func AIDeleteAPIs(credential credentials.Credential, CmdUploadEnvironment, aiTok
headers["API-KEY"] = AIToken
}

Endpoint := utils.GetAIServiceEndpointOfEnv(CmdUploadEnvironment, utils.MainConfigFilePath)
if (oldEndpoint != "") {
Endpoint = oldEndpoint
} else {
Endpoint = utils.GetAIServiceEndpointOfEnv(CmdPurgeEnvironment, utils.MainConfigFilePath)
}

fmt.Println("Removing existing APIs and API Products from vector database for tenant:", tenant)

Expand Down
16 changes: 12 additions & 4 deletions import-export-cli/impl/aiUploadApis.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (

var apiListQueue = make(chan []map[string]interface{}, 10)

func AIUploadAPIs(credential credentials.Credential, cmdUploadEnvironment, aiToken string, uploadAll, uploadProducts bool) {
func AIUploadAPIs(credential credentials.Credential, cmdUploadEnvironment, aiToken, oldEndpoint string, uploadAll, uploadProducts bool) {

CmdUploadEnvironment = cmdUploadEnvironment
Credential = credential
Expand All @@ -32,7 +32,11 @@ func AIUploadAPIs(credential credentials.Credential, cmdUploadEnvironment, aiTok
Tenant = strings.Split(credential.Username, "@")[1]
}

Endpoint = utils.GetAIServiceEndpointOfEnv(cmdUploadEnvironment, utils.MainConfigFilePath)
if (oldEndpoint != "") {
Endpoint = oldEndpoint
} else {
Endpoint = utils.GetAIServiceEndpointOfEnv(CmdUploadEnvironment, utils.MainConfigFilePath)
}

headers := make(map[string]string)
headers[utils.HeaderContentType] = utils.HeaderValueApplicationJSON
Expand All @@ -44,15 +48,19 @@ func AIUploadAPIs(credential credentials.Credential, cmdUploadEnvironment, aiTok
headers["API-KEY"] = AIToken
}

accessToken, err := credentials.GetOAuthAccessToken(credential, cmdUploadEnvironment)
accessToken, err := credentials.GetOAuthAccessToken(credential, CmdUploadEnvironment)

if err != nil {
utils.HandleErrorAndExit("Error getting OAuth Tokens", err)
}

ProduceAPIPayloads(accessToken, apiListQueue)

numConsumers := utils.AIThreadCount
numConsumers := utils.DefaultAIThreadCount
configVars := utils.GetMainConfigFromFile(utils.MainConfigFilePath)
if configVars.Config.AIThreadCount != 0 {
numConsumers = configVars.Config.AIThreadCount
}
var wg sync.WaitGroup
for i := 0; i < numConsumers; i++ {
wg.Add(1)
Expand Down
24 changes: 24 additions & 0 deletions import-export-cli/shell-completions/apictl_bash_completions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ _apictl_ai_delete_artifacts()
flags_with_completion=()
flags_completion=()

flags+=("--endpoint=")
two_word_flags+=("--endpoint")
local_nonpersistent_flags+=("--endpoint")
local_nonpersistent_flags+=("--endpoint=")
flags+=("--environment=")
two_word_flags+=("--environment")
two_word_flags+=("-e")
Expand All @@ -504,6 +508,10 @@ _apictl_ai_delete_artifacts()
flags+=("-h")
local_nonpersistent_flags+=("--help")
local_nonpersistent_flags+=("-h")
flags+=("--token=")
two_word_flags+=("--token")
local_nonpersistent_flags+=("--token")
local_nonpersistent_flags+=("--token=")
flags+=("--insecure")
flags+=("-k")
flags+=("--verbose")
Expand Down Expand Up @@ -608,6 +616,10 @@ _apictl_ai_upload_api-products()

flags+=("--all")
local_nonpersistent_flags+=("--all")
flags+=("--endpoint=")
two_word_flags+=("--endpoint")
local_nonpersistent_flags+=("--endpoint")
local_nonpersistent_flags+=("--endpoint=")
flags+=("--environment=")
two_word_flags+=("--environment")
two_word_flags+=("-e")
Expand All @@ -618,6 +630,10 @@ _apictl_ai_upload_api-products()
flags+=("-h")
local_nonpersistent_flags+=("--help")
local_nonpersistent_flags+=("-h")
flags+=("--token=")
two_word_flags+=("--token")
local_nonpersistent_flags+=("--token")
local_nonpersistent_flags+=("--token=")
flags+=("--insecure")
flags+=("-k")
flags+=("--verbose")
Expand Down Expand Up @@ -645,6 +661,10 @@ _apictl_ai_upload_apis()

flags+=("--all")
local_nonpersistent_flags+=("--all")
flags+=("--endpoint=")
two_word_flags+=("--endpoint")
local_nonpersistent_flags+=("--endpoint")
local_nonpersistent_flags+=("--endpoint=")
flags+=("--environment=")
two_word_flags+=("--environment")
two_word_flags+=("-e")
Expand All @@ -655,6 +675,10 @@ _apictl_ai_upload_apis()
flags+=("-h")
local_nonpersistent_flags+=("--help")
local_nonpersistent_flags+=("-h")
flags+=("--token=")
two_word_flags+=("--token")
local_nonpersistent_flags+=("--token")
local_nonpersistent_flags+=("--token=")
flags+=("--insecure")
flags+=("-k")
flags+=("--verbose")
Expand Down
2 changes: 1 addition & 1 deletion import-export-cli/utils/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type EnvEndpoints struct {
TokenEndpoint string `yaml:"token"`
MiManagementEndpoint string `yaml:"mi"`
AIServiceEndpoint string `yaml:"ai_service"`
AITokenServiceEndpoint string `yaml:"ai_token_service"`
AITokenServiceEndpoint string `yaml:"ai_token_endpoint"`
AIKey string `yaml:"ai_key"`
}

Expand Down

0 comments on commit 17918a3

Please sign in to comment.