-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #191 from perfectsense/feature/remote-backend-fix
Enables Remote State Storage
- Loading branch information
Showing
2 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package gyro.google; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
|
||
import com.google.api.services.storage.Storage; | ||
import com.google.api.services.storage.model.Objects; | ||
import com.google.api.services.storage.model.StorageObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class StorageObjectIterator implements Iterator<StorageObject> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(StorageObjectIterator.class); | ||
|
||
private String bucket; | ||
private String prefix; | ||
private Storage client; | ||
|
||
private List<StorageObject> storageObjects = new ArrayList<>(); | ||
private int index; | ||
private String nextPageToken; | ||
|
||
public StorageObjectIterator(String bucket, String prefix, Storage client) { | ||
this.bucket = bucket; | ||
this.prefix = prefix; | ||
this.client = client; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (index < storageObjects.size()) { | ||
return true; | ||
} | ||
|
||
if (index > 0 && nextPageToken == null) { | ||
return false; | ||
} | ||
|
||
try { | ||
Objects objects = client.objects() | ||
.list(bucket) | ||
.setPrefix(prefix) | ||
.setPageToken(nextPageToken) | ||
.setMaxResults(100L) | ||
.execute(); | ||
|
||
if (objects.getItems() != null) { | ||
index = 0; | ||
storageObjects.clear(); | ||
storageObjects.addAll(objects.getItems()); | ||
} | ||
nextPageToken = objects.getNextPageToken(); | ||
|
||
} catch (IOException e) { | ||
LOGGER.error("Failed to retrieve storage objects: {} {} {}", bucket, prefix, nextPageToken, e); | ||
return false; | ||
} | ||
|
||
return !storageObjects.isEmpty(); | ||
|
||
} | ||
|
||
@Override | ||
public StorageObject next() { | ||
if (hasNext()) { | ||
return storageObjects.get(index++); | ||
} | ||
throw new NoSuchElementException(); | ||
} | ||
} |