Skip to content

Commit

Permalink
remove support for passing values on object_add
Browse files Browse the repository at this point in the history
It makes the code unnecessarily complex and the approach won't work well
with objects spread over multiple plugins

Signed-off-by: Felix Fietkau <nbd@nbd.name>
  • Loading branch information
nbd168 committed Feb 14, 2017
1 parent 69703aa commit 51477c9
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 65 deletions.
10 changes: 1 addition & 9 deletions acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,12 @@ scald_acl_req_add_object(struct scapi_ptr *ptr)
}

void
scald_acl_req_add_new_instance(const char *name, struct kvlist *kv)
scald_acl_req_add_new_instance(const char *name)
{
const char *value;
void *c;

if (!acl_object.has_subscribers)
return;

blobmsg_add_string(&b, "name", name);

c = blobmsg_open_table(&b, "values");
kvlist_for_each(kv, name, value)
blobmsg_add_string(&b, name, value);
blobmsg_close_table(&b, c);
}

void
Expand Down
2 changes: 0 additions & 2 deletions cli
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ cmd_add() {

set_path "$path"
json_add_string name "$name"
json_add_object values
json_close_object
ubus_call add
}

Expand Down
45 changes: 4 additions & 41 deletions model.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,12 @@ static const struct blobmsg_policy obj_policy[__M_OBJ_MAX] = {
[M_OBJ_PATH] = { "path", BLOBMSG_TYPE_ARRAY },
[M_OBJ_NAME] = { "name", BLOBMSG_TYPE_STRING },
[M_OBJ_VALUE] = { "value", BLOBMSG_TYPE_STRING },
[M_OBJ_VALUES] = { "values", BLOBMSG_TYPE_TABLE },
};

#define M_OBJ_MASK (1 << M_OBJ_PATH)
#define M_GET_MASK (M_OBJ_MASK | (1 << M_OBJ_NAME))
#define M_SET_MASK (M_GET_MASK | (1 << M_OBJ_VALUE))
#define M_ADD_MASK (M_GET_MASK | (1 << M_OBJ_VALUES))
#define M_ADD_MASK M_GET_MASK


static const char *error_strings[] = {
Expand Down Expand Up @@ -384,22 +383,6 @@ scald_model_handle_set(struct ubus_context *ctx, struct ubus_object *obj,
return 0;
}

static int
scald_kvlist_add_strings(struct kvlist *kv, struct blob_attr *attr)
{
struct blob_attr *cur;
int rem;

blobmsg_for_each_attr(cur, attr, rem) {
if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
return SC_ERR_INVALID_ARGUMENT;

kvlist_set(kv, blobmsg_name(cur), blobmsg_get_string(cur));
}

return 0;
}

static int
scald_model_handle_add(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
Expand All @@ -410,14 +393,13 @@ scald_model_handle_add(struct ubus_context *ctx, struct ubus_object *obj,
struct blob_attr *tb[__M_OBJ_MAX];
struct scapi_list_ctx lctx;
int ret = SC_ERR_NOT_FOUND;
KVLIST(kv, kvlist_strlen);
const char *name;
void *c;

blobmsg_parse(obj_policy, __M_OBJ_MAX, tb, blob_data(msg), blob_len(msg));

ptr.path = tb[M_OBJ_PATH];
if (!ptr.path || !tb[M_OBJ_NAME] || !tb[M_OBJ_VALUES])
if (!ptr.path || !tb[M_OBJ_NAME])
return UBUS_STATUS_INVALID_ARGUMENT;

scald_acl_req_init(req, method);
Expand All @@ -431,22 +413,6 @@ scald_model_handle_add(struct ubus_context *ctx, struct ubus_object *obj,
blobmsg_close_array(&b, c);
ptr.path = blob_data(b.head);

scald_model_iterate_plugins(&lctx, &ptr) {
if (ptr.plugin->object_get(&ptr) == 0) {
ret = SC_ERR_ALREADY_EXISTS;
goto out;
}

if (!ptr.plugin->object_get_defaults)
continue;

ptr.plugin->object_get_defaults(&ptr, &kv);
}

ret = scald_kvlist_add_strings(&kv, tb[M_OBJ_VALUES]);
if (ret)
goto out;

ret = SC_ERR_NOT_FOUND;
ptr.path = tb[M_OBJ_PATH];
scald_model_iterate_plugins(&lctx, &ptr) {
Expand All @@ -460,23 +426,20 @@ scald_model_handle_add(struct ubus_context *ctx, struct ubus_object *obj,

scald_acl_req_prepare(&ptr);
scald_acl_req_add_object(&ptr);
scald_acl_req_add_new_instance(name, &kv);
scald_acl_req_add_new_instance(name);
if (scald_acl_req_check(&ptr)) {
ret = SC_ERR_ACCESS_DENIED;
break;
}

cur_ret = ptr.plugin->object_add(&ptr, name, &kv);
cur_ret = ptr.plugin->object_add(&ptr, name);
if (cur_ret == SC_ERR_NOT_SUPPORTED)
continue;

ret = cur_ret;
break;
}

out:
kvlist_free(&kv);

if (ret) {
blob_buf_init(&b, 0);
scald_report_error(&b, "error", ret);
Expand Down
2 changes: 1 addition & 1 deletion plugins/json/plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ sj_api_object_list(struct scapi_ptr *ptr, scapi_object_cb fill,
}

static int
sj_api_object_add(struct scapi_ptr *ptr, const char *name, struct kvlist *values)
sj_api_object_add(struct scapi_ptr *ptr, const char *name)
{
struct sj_model *model = ptr->model_priv;
struct sj_object *obj;
Expand Down
2 changes: 1 addition & 1 deletion scald.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void scald_acl_req_init(struct ubus_request_data *req, const char *method);
void scald_acl_req_prepare(struct scapi_ptr *ptr);
void scald_acl_req_add_object(struct scapi_ptr *ptr);
void scald_acl_req_add_param(struct scapi_ptr *ptr);
void scald_acl_req_add_new_instance(const char *name, struct kvlist *kv);
void scald_acl_req_add_new_instance(const char *name);
int scald_acl_req_check(struct scapi_ptr *ptr);
void scald_acl_req_done(void);

Expand Down
12 changes: 1 addition & 11 deletions scapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,19 @@ struct scapi_plugin {
*/
int (*object_get)(struct scapi_ptr *ptr);

/*
* Fetch defaults for a newly created instance of an object by path
*
* ptr: reference to plugin, data model, path to the parent object
* of the new instance
* kv: string key/value list to store defaults
*/
int (*object_get_defaults)(struct scapi_ptr *ptr, struct kvlist *kv);

/*
* Create a new object instance
*
* ptr: reference to plugin, data model, path to the parent object
* name: name of the new object instance
* values: default values for the object
*
* Must return SC_ERR_NOT_SUPPORTED if the plugin does not support creating
* an object on the specified path and another plugin might.
*
* All other errors are treated as fatal and do not allow deferring to a
* different plugin.
*/
int (*object_add)(struct scapi_ptr *ptr, const char *name, struct kvlist *values);
int (*object_add)(struct scapi_ptr *ptr, const char *name);

/*
* Delete an object instance
Expand Down

0 comments on commit 51477c9

Please sign in to comment.