Skip to content
This repository has been archived by the owner on Mar 25, 2018. It is now read-only.

Adds User feature to the Reddwarf APIs #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@

import java.io.Closeable;
import java.util.Set;

import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.Zone;
import org.jclouds.location.functions.ZoneToEndpoint;
import org.jclouds.openstack.keystone.v2_0.domain.Tenant;
import org.jclouds.openstack.reddwarf.v1.features.FlavorApi;
import org.jclouds.openstack.reddwarf.v1.features.InstanceApi;
import org.jclouds.openstack.reddwarf.v1.features.UserApi;
import org.jclouds.rest.annotations.Delegate;
import org.jclouds.rest.annotations.EndpointParam;

import com.google.common.base.Optional;
import com.google.inject.Provides;

Expand Down Expand Up @@ -61,6 +60,13 @@ FlavorApi getFlavorApiForZone(
InstanceApi getInstanceApiForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);

/**
* Provides access to User features.
*/
@Delegate
UserApi getUserApiForZone(
@EndpointParam(parser = ZoneToEndpoint.class) @Nullable String zone);

/**
* Provides the Tenant
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.redddwarf.v1.binders;
package org.jclouds.openstack.reddwarf.v1.binders;

import java.util.Map;
import org.jclouds.http.HttpRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.reddwarf.v1.binders;

import java.util.Map;
import java.util.Set;

import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.reddwarf.v1.domain.Database;
import org.jclouds.openstack.reddwarf.v1.domain.User;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;

/**
* @author Zack Shoylev
*/
public class BindCreateUserToJson implements MapBinder {

@Inject
private BindToJsonPayload jsonBinder;

@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
Set<User> users = Sets.newHashSet();
if( postParams.get("name") != null ) {
Database database = Database.builder().name((String) postParams.get("databaseName")).build();
Set<Database> databases = Sets.newHashSet();
databases.add(database);
User user = User.builder()
.name((String) postParams.get("name"))
.password((String) postParams.get("password"))
.databases(databases)
.build();
users.add(user);
}
else if( postParams.get("users") != null ) {
users = (Set<User>) postParams.get("users");
}
return jsonBinder.bindToRequest(request, ImmutableMap.of("users", users));
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("Create user is a POST operation");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.reddwarf.v1.binders;

import java.util.Map;
import java.util.Set;

import org.jclouds.http.HttpRequest;
import org.jclouds.openstack.reddwarf.v1.domain.Database;
import org.jclouds.rest.MapBinder;
import org.jclouds.rest.binders.BindToJsonPayload;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;
import com.google.inject.Inject;

/**
* @author Zack Shoylev
*/
public class BindGrantUserToJson implements MapBinder {

@Inject
private BindToJsonPayload jsonBinder;

@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Map<String, Object> postParams) {
Set<Database> databases = Sets.newHashSet();
if( postParams.get("databaseName")!=null ) {
Database database = Database.builder().name((String)postParams.get("databaseName")).build();
databases.add(database);
}
else if( postParams.get("databases")!=null ) {
databases = (Set<Database>) postParams.get("databases");
}
return jsonBinder.bindToRequest(request, ImmutableMap.of("databases", databases));
}

@Override
public <R extends HttpRequest> R bindToRequest(R request, Object toBind) {
throw new IllegalStateException("Grant user is a PUT operation");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.openstack.reddwarf.v1.domain;

import static com.google.common.base.Preconditions.checkNotNull;
import java.beans.ConstructorProperties;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;

/**
* An Openstack Reddwarf Database.
*
* @author Zack Shoylev
*/
public class Database implements Comparable<Database>{

private final String name;

@ConstructorProperties({
"name"
})
protected Database(String name) {
this.name = checkNotNull(name, "name required");
}

/**
* @return the name of this database
* @see Database.Builder#name(String)
*/
public String getName() {
return this.name;
}

@Override
public int hashCode() {
return Objects.hashCode(name);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Database that = Database.class.cast(obj);
return Objects.equal(this.name, that.name);
}

protected ToStringHelper string() {
return Objects.toStringHelper(this)
.add("name", name);
}

@Override
public String toString() {
return string().toString();
}

public static Builder builder() {
return new Builder();
}

public Builder toBuilder() {
return new Builder().fromDatabase(this);
}

public static class Builder {
protected String name;

/**
* @param name The name of this database
* @return The builder object
* @see Database#getName()
*/
public Builder name(String name) {
this.name = name;
return this;
}

/**
*
* @return A new Database object
*/
public Database build() {
return new Database(name);
}

public Builder fromDatabase(Database in) {
return this
.name(in.getName());
}
}

@Override
public int compareTo(Database that) {
return this.getName().compareTo(that.getName());
}
}
Loading