001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Mariana Cedica
018 */
019package org.nuxeo.ecm.core.storage.status;
020
021import java.util.Map;
022
023import org.nuxeo.ecm.core.blob.BlobManager;
024import org.nuxeo.ecm.core.blob.BlobProvider;
025import org.nuxeo.ecm.core.blob.binary.BinaryManager;
026import org.nuxeo.ecm.core.management.api.Probe;
027import org.nuxeo.ecm.core.management.api.ProbeStatus;
028import org.nuxeo.ecm.core.storage.sql.S3BinaryManager;
029import org.nuxeo.runtime.api.Framework;
030
031/**
032 * Probe to check the status of the S3BinaryManager. Returns success if Nuxeo can access the bucket, failure otherwise
033 *
034 * @since 9.3
035 */
036public class S3BinaryManagerStatusProbe implements Probe {
037
038    @Override
039    public ProbeStatus run() {
040        Map<String, BlobProvider> providers = Framework.getService(BlobManager.class).getBlobProviders();
041        boolean someSuccess = false;
042        for (Map.Entry<String, BlobProvider> en : providers.entrySet()) {
043            String id = en.getKey();
044            BlobProvider blobProvider = en.getValue();
045            BinaryManager bm = blobProvider.getBinaryManager();
046            if (bm == null || !(bm instanceof S3BinaryManager)) {
047                continue;
048            }
049            if (!((S3BinaryManager) bm).canAccessBucket()) {
050                return ProbeStatus.newFailure("S3BinaryManager cannot access the configured bucket: " + id);
051            }
052            someSuccess = true;
053        }
054        if (someSuccess) {
055            return ProbeStatus.newSuccess("S3BinaryManager can access the configured buckets");
056        } else {
057            return ProbeStatus.newFailure("No S3BinaryManager bucket configured");
058        }
059    }
060
061}