Search in sources :

Example 1 with YamlMapping

use of com.amihaiemil.eoyaml.YamlMapping in project front by artipie.

the class YamlRepoPermissions method add.

@Override
public void add(final String repo, final String uid, final JsonArray perms) {
    final Optional<YamlMapping> creds = this.permsYaml(repo);
    YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
    if (creds.isPresent()) {
        for (final YamlNode node : creds.get().keys()) {
            final String usr = node.asScalar().value();
            if (!uid.equals(usr)) {
                builder = builder.add(usr, creds.get().yamlSequence(usr));
            }
        }
    }
    YamlSequenceBuilder seq = Yaml.createYamlSequenceBuilder();
    for (final String item : perms.getValuesAs(JsonValue::toString)) {
        seq = seq.add(item);
    }
    builder = builder.add(uid, seq.build());
    this.updateRepoSettings(repo, builder.build());
}
Also used : YamlNode(com.amihaiemil.eoyaml.YamlNode) JsonValue(javax.json.JsonValue) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlSequenceBuilder(com.amihaiemil.eoyaml.YamlSequenceBuilder)

Example 2 with YamlMapping

use of com.amihaiemil.eoyaml.YamlMapping in project front by artipie.

the class YamlRepoPermissions method updateRepoSettings.

/**
 * Updates credentials section in repository settings.
 * @param repo Repository name
 * @param creds New credentials section
 */
private void updateRepoSettings(final String repo, final YamlMapping creds) {
    try {
        final YamlMapping stngs = Yaml.createYamlInput(new String(this.blsto.value(this.key(repo)), StandardCharsets.UTF_8)).readYamlMapping().yamlMapping(YamlRepoPermissions.REPO);
        YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
        for (final YamlNode node : stngs.keys()) {
            final String map = node.asScalar().value();
            if (!YamlRepoPermissions.PERMS.equals(map)) {
                builder = builder.add(map, creds.yamlSequence(map));
            }
        }
        builder = builder.add(YamlRepoPermissions.PERMS, creds);
        this.blsto.save(this.key(repo), Yaml.createYamlMappingBuilder().add(YamlRepoPermissions.REPO, builder.build()).build().toString().getBytes(StandardCharsets.UTF_8));
    } catch (final IOException err) {
        throw new UncheckedIOException(err);
    }
}
Also used : YamlNode(com.amihaiemil.eoyaml.YamlNode) UncheckedIOException(java.io.UncheckedIOException) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 3 with YamlMapping

use of com.amihaiemil.eoyaml.YamlMapping in project front by artipie.

the class YamlStorage method storage.

/**
 * Provides a storage.
 *
 * @return Storage instance.
 */
public Storage storage() {
    @SuppressWarnings("deprecation") final YamlMapping strict = new StrictYamlMapping(this.yaml);
    final String type = strict.string("type");
    final Storage storage;
    if ("fs".equals(type)) {
        storage = new FileStorage(Path.of(strict.string("path")));
    } else if ("s3".equals(type)) {
        storage = new S3Storage(this.s3Client(), strict.string("bucket"), !"false".equals(this.yaml.string("multipart")));
    } else if ("etcd".equals(type)) {
        storage = new EtcdStorage(YamlStorage.etcdClient(strict.yamlMapping("connection")));
    } else {
        throw new IllegalStateException(String.format("Unsupported storage type: '%s'", type));
    }
    return storage;
}
Also used : StrictYamlMapping(com.amihaiemil.eoyaml.StrictYamlMapping) EtcdStorage(com.artipie.asto.etcd.EtcdStorage) Storage(com.artipie.asto.Storage) FileStorage(com.artipie.asto.fs.FileStorage) S3Storage(com.artipie.asto.s3.S3Storage) FileStorage(com.artipie.asto.fs.FileStorage) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) StrictYamlMapping(com.amihaiemil.eoyaml.StrictYamlMapping) EtcdStorage(com.artipie.asto.etcd.EtcdStorage) S3Storage(com.artipie.asto.s3.S3Storage)

Example 4 with YamlMapping

use of com.amihaiemil.eoyaml.YamlMapping in project front by artipie.

the class YamlStorages method storages.

/**
 * Returns storages yaml mapping if found.
 * @return Settings storages yaml
 */
private Optional<YamlMapping> storages() {
    final Optional<Key> key = this.key();
    Optional<YamlMapping> res = Optional.empty();
    if (key.isPresent()) {
        try {
            res = Optional.ofNullable(Yaml.createYamlInput(new String(this.blsto.value(key.get()), StandardCharsets.UTF_8)).readYamlMapping().yamlMapping(YamlStorages.STORAGES_NODE));
        } catch (final IOException err) {
            throw new UncheckedIOException(err);
        }
    }
    return res;
}
Also used : UncheckedIOException(java.io.UncheckedIOException) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Key(com.artipie.asto.Key)

Example 5 with YamlMapping

use of com.amihaiemil.eoyaml.YamlMapping in project front by artipie.

the class YamlStorages method add.

@Override
public void add(final String alias, final JsonObject info) {
    YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
    final Optional<YamlMapping> storages = this.storages();
    if (storages.isPresent()) {
        for (final YamlNode node : storages.get().keys()) {
            final String name = node.asScalar().value();
            builder = builder.add(name, storages.get().yamlMapping(name));
        }
    }
    builder = builder.add(alias, new Json2Yaml().apply(info.toString()));
    this.blsto.save(this.key().orElse(this.repo.<Key>map(val -> new Key.From(val, YamlStorages.YAML)).orElse(YamlStorages.YAML)), Yaml.createYamlMappingBuilder().add(YamlStorages.STORAGES_NODE, builder.build()).build().toString().getBytes(StandardCharsets.UTF_8));
}
Also used : JsonObject(javax.json.JsonObject) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) Collection(java.util.Collection) Yaml2Json(com.artipie.front.misc.Yaml2Json) IOException(java.io.IOException) Key(com.artipie.asto.Key) BlockingStorage(com.artipie.asto.blocking.BlockingStorage) Yaml(com.amihaiemil.eoyaml.Yaml) YamlNode(com.amihaiemil.eoyaml.YamlNode) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) Optional(java.util.Optional) NotFoundException(com.artipie.front.api.NotFoundException) Json2Yaml(com.artipie.front.misc.Json2Yaml) Collections(java.util.Collections) Json2Yaml(com.artipie.front.misc.Json2Yaml) YamlNode(com.amihaiemil.eoyaml.YamlNode) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlMapping(com.amihaiemil.eoyaml.YamlMapping)

Aggregations

YamlMapping (com.amihaiemil.eoyaml.YamlMapping)23 YamlNode (com.amihaiemil.eoyaml.YamlNode)10 Key (com.artipie.asto.Key)9 YamlMappingBuilder (com.amihaiemil.eoyaml.YamlMappingBuilder)8 Storage (com.artipie.asto.Storage)7 Collectors (java.util.stream.Collectors)6 IOException (java.io.IOException)5 UncheckedIOException (java.io.UncheckedIOException)5 StrictYamlMapping (com.amihaiemil.eoyaml.StrictYamlMapping)4 Yaml (com.amihaiemil.eoyaml.Yaml)4 BlockingStorage (com.artipie.asto.blocking.BlockingStorage)4 EtcdStorage (com.artipie.asto.etcd.EtcdStorage)4 FileStorage (com.artipie.asto.fs.FileStorage)4 S3Storage (com.artipie.asto.s3.S3Storage)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 YamlSequenceBuilder (com.amihaiemil.eoyaml.YamlSequenceBuilder)3 NotFoundException (com.artipie.front.api.NotFoundException)3 Json2Yaml (com.artipie.front.misc.Json2Yaml)3