package main

import (
	"context"
	"fmt"
	"log"

	"github.com/tidbcloud/tiinf/sdk/rbac/useadapter"
)

// Example_DirectCall_CheckIdentityPermission example for direct call permission check api
func Example_DirectCall_CheckIdentityPermission() {
	// identity
	identity := "12345"
	// request relate resource_uri
	resource := "/orgs/{org_id}/projects/{project_id}/list"
	// params in resource_uri, for example: org_id、project_id，if resource has no variable, the resourceParam can be empty
	resourceParam := map[string]string{
		// key must be variable in resource_uri,
		"org_id":     "xxx_1",
		"project_id": "xxx_2",
	}
	hasPermission, msg, err := useadapter.CheckIdentityPermission(context.Background(), identity, resource, resourceParam)
	log.Println(fmt.Sprintf("permission check over, hasPermisson: [%v], msg: [%s], err: [%v]", hasPermission, msg, err))
}

func Example_DirectCall_CheckApiKeyPermission() {
	// identity
	identity := "12345"
	// request relate resource_uri
	resource := "/dataapi/dataapps/{app_id}/action"
	// params in resource_uri, for example: org_id、project_id，if resource has no variable, the resourceParam can be empty
	resourceParam := map[string]string{
		// key must be variable in resource_uri,
		"app_id": "xxx_1",
	}
	hasPermission, msg, err := useadapter.CheckApiKeyPermission(context.Background(), identity, resource, resourceParam)
	log.Println(fmt.Sprintf("permission check over, hasPermisson: [%v], msg: [%s], err: [%v]", hasPermission, msg, err))
}

func Example_DirectCall_CheckRegionApiKeyPermission() {
	// identity
	identity := "access_key1"
	// request relate resource_uri
	resource := "/dataapi/dataapps/{app_id}/action"
	// params in resource_uri, for example: org_id、project_id，if resource has no variable, the resourceParam can be empty
	resourceParam := map[string]string{
		// key must be variable in resource_uri,
		"app_id": "xxx_1",
	}
	hasPermission, msg, err := useadapter.CheckRegionApiKeyPermission(context.Background(), identity, resource, resourceParam)
	log.Println(fmt.Sprintf("permission check over, hasPermisson: [%v], msg: [%s], err: [%v]", hasPermission, msg, err))
}
