2020-08-14 10:56:05 -07:00
|
|
|
#include "../include/apiClient.h"
|
|
|
|
|
#include "../include/generic.h"
|
2021-09-09 10:08:30 -03:00
|
|
|
#include "../include/utils.h"
|
2021-09-06 18:18:47 -03:00
|
|
|
|
2020-08-14 10:56:05 -07:00
|
|
|
genericClient_t* genericClient_create(apiClient_t *client, const char *apiGroup, const char* apiVersion, const char* resourcePlural) {
|
|
|
|
|
genericClient_t *result = malloc(sizeof(genericClient_t));
|
|
|
|
|
result->client = client;
|
2023-04-24 11:12:35 +03:00
|
|
|
result->apiGroup = apiGroup ? strdup(apiGroup) : NULL;
|
2020-08-14 10:56:05 -07:00
|
|
|
result->apiVersion = strdup(apiVersion);
|
|
|
|
|
result->resourcePlural = strdup(resourcePlural);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void genericClient_free(genericClient_t* client) {
|
|
|
|
|
free(client->apiGroup);
|
|
|
|
|
free(client->apiVersion);
|
|
|
|
|
free(client->resourcePlural);
|
|
|
|
|
free(client);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void makeNamespacedResourcePath(char* path, genericClient_t *client, const char* namespace, const char* name) {
|
2020-08-14 12:24:40 -07:00
|
|
|
if (!client->apiGroup || strlen(client->apiGroup) == 0) {
|
|
|
|
|
snprintf(path, 128, "/api/%s/namespaces/%s/%s/%s",
|
|
|
|
|
client->apiVersion, namespace, client->resourcePlural, name);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(
|
|
|
|
|
path, 128,
|
|
|
|
|
"/apis/%s/%s/namespaces/%s/%s/%s",
|
|
|
|
|
client->apiGroup,
|
|
|
|
|
client->apiVersion,
|
|
|
|
|
namespace,
|
|
|
|
|
client->resourcePlural,
|
|
|
|
|
name
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-14 10:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void makeResourcePath(char* path, genericClient_t *client, const char* name) {
|
2020-08-14 12:24:40 -07:00
|
|
|
if (!client->apiGroup || strlen(client->apiGroup) == 0) {
|
|
|
|
|
snprintf(path, 128, "/api/%s/%s/%s", client->apiVersion, client->resourcePlural, name);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(
|
|
|
|
|
path, 128,
|
|
|
|
|
"/apis/%s/%s/%s/%s",
|
|
|
|
|
client->apiGroup,
|
|
|
|
|
client->apiVersion,
|
|
|
|
|
client->resourcePlural,
|
|
|
|
|
name
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-14 10:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
2021-02-18 16:48:49 +08:00
|
|
|
char* callInternal(genericClient_t *client,
|
|
|
|
|
const char *path, list_t *queryParameters, list_t *headerParameters, list_t *formParameters, list_t *headerType, list_t *contentType, const char *body, const char *method)
|
|
|
|
|
{
|
|
|
|
|
apiClient_invoke(client->client, path, queryParameters, headerParameters, formParameters, headerType, contentType, body, method);
|
2020-08-14 10:56:05 -07:00
|
|
|
|
|
|
|
|
if (client->client->response_code == 200) {
|
|
|
|
|
printf("%s\n","OK");
|
|
|
|
|
}
|
|
|
|
|
if (client->client->response_code == 401) {
|
|
|
|
|
printf("%s\n","Unauthorized");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
char* elementToReturn = strndup((char*)client->client->dataReceived, client->client->dataReceivedLen);
|
|
|
|
|
|
|
|
|
|
if (client->client->dataReceived) {
|
|
|
|
|
free(client->client->dataReceived);
|
|
|
|
|
client->client->dataReceived = NULL;
|
|
|
|
|
client->client->dataReceivedLen = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return elementToReturn;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-18 16:48:49 +08:00
|
|
|
char *callSimplifiedInternal(genericClient_t *client, const char *path, const char *method, const char *body)
|
|
|
|
|
{
|
|
|
|
|
return callInternal(client, path, NULL, NULL, NULL, NULL, NULL, body, method);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 10:56:05 -07:00
|
|
|
char* Generic_readNamespacedResource(genericClient_t *client, const char *namespace, const char *name) {
|
|
|
|
|
char path[128];
|
|
|
|
|
|
|
|
|
|
makeNamespacedResourcePath(path, client, namespace, name);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "GET", NULL);
|
2020-08-14 10:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_readResource(genericClient_t *client, const char *name) {
|
|
|
|
|
char path[128];
|
|
|
|
|
|
|
|
|
|
makeResourcePath(path, client, name);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "GET", NULL);
|
2020-08-14 10:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *Generic_listNamespaced(genericClient_t *client, const char *namespace) {
|
|
|
|
|
char path[128];
|
2023-04-21 12:49:18 +03:00
|
|
|
if (client->apiGroup && strlen(client->apiGroup)) {
|
2020-08-14 12:24:40 -07:00
|
|
|
snprintf(path, 128, "/apis/%s/%s/namespaces/%s/%s",
|
|
|
|
|
client->apiGroup, client->apiVersion, namespace, client->resourcePlural);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(path, 128, "/api/%s/namespaces/%s/%s",
|
|
|
|
|
client->apiVersion, namespace, client->resourcePlural);
|
|
|
|
|
}
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "GET", NULL);
|
2020-08-14 10:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *Generic_list(genericClient_t *client) {
|
|
|
|
|
char path[128];
|
2023-04-21 12:49:18 +03:00
|
|
|
if (client->apiGroup && strlen(client->apiGroup)) {
|
2020-08-14 12:24:40 -07:00
|
|
|
snprintf(path, 128, "/apis/%s/%s/%s",
|
|
|
|
|
client->apiGroup, client->apiVersion, client->resourcePlural);
|
|
|
|
|
} else {
|
|
|
|
|
snprintf(path, 128, "/api/%s/%s",
|
|
|
|
|
client->apiVersion, client->resourcePlural);
|
|
|
|
|
}
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "GET", NULL);
|
2020-08-14 12:24:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_deleteNamespacedResource(genericClient_t *client, const char *namespace, const char *name) {
|
|
|
|
|
char path[128];
|
|
|
|
|
makeNamespacedResourcePath(path, client, namespace, name);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "DELETE", NULL);
|
2020-08-14 12:24:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_deleteResource(genericClient_t *client, const char* name) {
|
|
|
|
|
char path[128];
|
|
|
|
|
makeResourcePath(path, client, name);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "DELETE", NULL);
|
2020-08-14 12:24:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_createNamespacedResource(genericClient_t *client, const char *ns, const char* body) {
|
|
|
|
|
char path[128];
|
|
|
|
|
makeNamespacedResourcePath(path, client, ns, "");
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "POST", body);
|
2020-08-14 12:24:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_createResource(genericClient_t *client, const char* body) {
|
|
|
|
|
char path[128];
|
|
|
|
|
makeResourcePath(path, client, "");
|
|
|
|
|
printf("%s\n", path);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "POST", body);
|
2020-08-14 10:56:05 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:24:40 -07:00
|
|
|
char* Generic_replaceNamespacedResource(genericClient_t *client, const char *ns, const char *name, const char* body) {
|
|
|
|
|
char path[128];
|
|
|
|
|
makeNamespacedResourcePath(path, client, ns, name);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "PUT", body);
|
2020-08-14 12:24:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_replaceResource(genericClient_t *client, const char *name, const char* body) {
|
|
|
|
|
char path[128];
|
|
|
|
|
makeResourcePath(path, client, name);
|
2021-02-18 16:48:49 +08:00
|
|
|
return callSimplifiedInternal(client, path, "PUT", body);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_patchNamespacedResource(genericClient_t * client,
|
|
|
|
|
const char *ns,
|
|
|
|
|
const char *name, const char *body, list_t *queryParameters, list_t *headerParameters, list_t *formParameters, list_t *headerType, list_t *contentType)
|
|
|
|
|
{
|
|
|
|
|
char path[128];
|
|
|
|
|
makeNamespacedResourcePath(path, client, ns, name);
|
|
|
|
|
return callInternal(client, path, queryParameters, headerParameters, formParameters, headerType, contentType, body, "PATCH");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char* Generic_patchResource(genericClient_t * client,
|
|
|
|
|
const char *name, const char *body, list_t *queryParameters, list_t *headerParameters, list_t *formParameters, list_t *headerType, list_t *contentType)
|
|
|
|
|
{
|
|
|
|
|
char path[128];
|
|
|
|
|
makeResourcePath(path, client, name);
|
|
|
|
|
return callInternal(client, path, queryParameters, headerParameters, formParameters, headerType, contentType, body, "PATCH");
|
2020-08-14 12:24:40 -07:00
|
|
|
}
|