func CreateResource(ctx context.Context, resource KeycloakResource, authzEndpoint string, protectionAPIToken string) (string, error) {
	log.Debug(ctx, map[string]interface{}{
		"resource": resource,
	}, "Creating a new Keycloak resource")

	b, err := json.Marshal(resource)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"resource": resource,
			"err":      err.Error(),
		}, "Unable to marshal keyclaok resource struct")
		return "", errors.NewInternalError("unable to marshal keyclaok resource struct " + err.Error())
	}

	req, err := http.NewRequest("POST", authzEndpoint, strings.NewReader(string(b)))
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"err": err.Error(),
		}, "Unable to crete http request")
		return "", errors.NewInternalError("unable to crete http request " + err.Error())
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer "+protectionAPIToken)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"resource": resource,
			"err":      err.Error(),
		}, "Unable to create a Keycloak resource")
		return "", errors.NewInternalError("Unable to create a Keycloak resource " + err.Error())
	}
	if res.StatusCode != http.StatusCreated {
		log.Error(ctx, map[string]interface{}{
			"resource":        resource,
			"response_status": res.Status,
			"response_body":   rest.ReadBody(res.Body),
		}, "Unable to create a Keycloak resource")
		return "", errors.NewInternalError("Unable to create a Keycloak resource. Response status: " + res.Status + ". Responce body: " + rest.ReadBody(res.Body))
	}
	jsonString := rest.ReadBody(res.Body)

	var r createResourceRequestResultPayload
	err = json.Unmarshal([]byte(jsonString), &r)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"resource":    resource,
			"json_string": jsonString,
		}, "Unable to unmarshal json with the create keycloak resource request result")

		return "", errors.NewInternalError(fmt.Sprintf("Unable to unmarshal json with the create keycloak resource request result %s ", jsonString) + err.Error())
	}

	log.Debug(ctx, map[string]interface{}{
		"resource_name": resource.Name,
		"resource_id":   r.ID,
	}, "Keycloak resource created")

	return r.ID, nil
}
// CreateResource creates a Keycloak resource
CreateResource creates a Keycloak resource
// CreateResource creates a Keycloak resource
func CreateResource(ctx context.Context, resource KeycloakResource, authzEndpoint string, protectionAPIToken string) (string, error) {
	log.Debug(ctx, map[string]interface{}{
		"resource": resource,
	}, "Creating a new Keycloak resource")
	b, err := json.Marshal(resource)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"resource": resource,
			"err":      err.Error(),
		}, "Unable to marshal keyclaok resource struct")
		return "", errors.NewInternalError("unable to marshal keyclaok resource struct " + err.Error())
	}
	req, err := http.NewRequest("POST", authzEndpoint, strings.NewReader(string(b)))
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"err": err.Error(),
		}, "Unable to crete http request")
		return "", errors.NewInternalError("unable to crete http request " + err.Error())
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer "+protectionAPIToken)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"resource": resource,
			"err":      err.Error(),
		}, "Unable to create a Keycloak resource")
		return "", errors.NewInternalError("Unable to create a Keycloak resource " + err.Error())
	}
	if res.StatusCode != http.StatusCreated {
		log.Error(ctx, map[string]interface{}{
			"resource":        resource,
			"response_status": res.Status,
			"response_body":   rest.ReadBody(res.Body),
		}, "Unable to create a Keycloak resource")
		return "", errors.NewInternalError("Unable to create a Keycloak resource. Response status: " + res.Status + ". Responce body: " + rest.ReadBody(res.Body))
	}
	jsonString := rest.ReadBody(res.Body)
	var r createResourceRequestResultPayload
	err = json.Unmarshal([]byte(jsonString), &r)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"resource":    resource,
			"json_string": jsonString,
		}, "Unable to unmarshal json with the create keycloak resource request result")
		return "", errors.NewInternalError(fmt.Sprintf("Unable to unmarshal json with the create keycloak resource request result %s ", jsonString) + err.Error())
	}
	log.Debug(ctx, map[string]interface{}{
		"resource_name": resource.Name,
		"resource_id":   r.ID,
	}, "Keycloak resource created")
	return r.ID, nil
}
func CreateResource ( ctx context . Context , resource KeycloakResource , authzEndpoint string , protectionAPIToken string ) ( string , error ) { log . Debug ( ctx , map [ string ] interface { } { " " : resource , } , " " ) b , err := json . Marshal ( resource ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : resource , " " : err . Error ( ) , } , " " ) return " " , errors . NewInternalError ( " " + err . Error ( ) ) } req , err := http . NewRequest ( " " , authzEndpoint , strings . NewReader ( string ( b ) ) ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : err . Error ( ) , } , " " ) return " " , errors . NewInternalError ( " " + err . Error ( ) ) } req . Header . Add ( " " , " " ) req . Header . Add ( " " , " " + protectionAPIToken ) res , err := http . DefaultClient . Do ( req ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : resource , " " : err . Error ( ) , } , " " ) return " " , errors . NewInternalError ( " " + err . Error ( ) ) } if res . StatusCode != http . StatusCreated { log . Error ( ctx , map [ string ] interface { } { " " : resource , " " : res . Status , " " : rest . ReadBody ( res . Body ) , } , " " ) return " " , errors . NewInternalError ( " " + res . Status + " " + rest . ReadBody ( res . Body ) ) } jsonString := rest . ReadBody ( res . Body ) var r createResourceRequestResultPayload err = json . Unmarshal ( [ ] byte ( jsonString ) , & r ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : resource , " " : jsonString , } , " " ) return " " , errors . NewInternalError ( fmt . Sprintf ( " " , jsonString ) + err . Error ( ) ) } log . Debug ( ctx , map [ string ] interface { } { " " : resource . Name , " " : r . ID , } , " " ) return r . ID , nil }
CreateResource creates a Keycloak resource
CreateResource creates a Keycloak resource