forked from kubernetes-client/c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic.c
167 lines (143 loc) · 6.25 KB
/
generic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "../include/apiClient.h"
#include "../include/generic.h"
#include "../include/utils.h"
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;
result->apiGroup = apiGroup ? strdup(apiGroup) : NULL;
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) {
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
);
}
}
void makeResourcePath(char* path, genericClient_t *client, const char* name) {
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
);
}
}
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);
if (client->client->response_code == 401) {
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;
}
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);
}
char* Generic_readNamespacedResource(genericClient_t *client, const char *namespace, const char *name) {
char path[128];
makeNamespacedResourcePath(path, client, namespace, name);
return callSimplifiedInternal(client, path, "GET", NULL);
}
char* Generic_readResource(genericClient_t *client, const char *name) {
char path[128];
makeResourcePath(path, client, name);
return callSimplifiedInternal(client, path, "GET", NULL);
}
char *Generic_listNamespaced(genericClient_t *client, const char *namespace) {
char path[128];
if (client->apiGroup && strlen(client->apiGroup)) {
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);
}
return callSimplifiedInternal(client, path, "GET", NULL);
}
char *Generic_list(genericClient_t *client) {
char path[128];
if (client->apiGroup && strlen(client->apiGroup)) {
snprintf(path, 128, "/apis/%s/%s/%s",
client->apiGroup, client->apiVersion, client->resourcePlural);
} else {
snprintf(path, 128, "/api/%s/%s",
client->apiVersion, client->resourcePlural);
}
return callSimplifiedInternal(client, path, "GET", NULL);
}
char* Generic_deleteNamespacedResource(genericClient_t *client, const char *namespace, const char *name) {
char path[128];
makeNamespacedResourcePath(path, client, namespace, name);
return callSimplifiedInternal(client, path, "DELETE", NULL);
}
char* Generic_deleteResource(genericClient_t *client, const char* name) {
char path[128];
makeResourcePath(path, client, name);
return callSimplifiedInternal(client, path, "DELETE", NULL);
}
char* Generic_createNamespacedResource(genericClient_t *client, const char *ns, const char* body) {
char path[128];
makeNamespacedResourcePath(path, client, ns, "");
return callSimplifiedInternal(client, path, "POST", body);
}
char* Generic_createResource(genericClient_t *client, const char* body) {
char path[128];
makeResourcePath(path, client, "");
return callSimplifiedInternal(client, path, "POST", body);
}
char* Generic_replaceNamespacedResource(genericClient_t *client, const char *ns, const char *name, const char* body) {
char path[128];
makeNamespacedResourcePath(path, client, ns, name);
return callSimplifiedInternal(client, path, "PUT", body);
}
char* Generic_replaceResource(genericClient_t *client, const char *name, const char* body) {
char path[128];
makeResourcePath(path, client, name);
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");
}