Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.

Commit 4de4926

Browse files
author
Fred Ross
committed
Merge pull request #2 from splunk/dfoster_feature/entity
PHP SDK: Load collections and entities
2 parents 4f7bd72 + 25f8911 commit 4de4926

19 files changed

+787
-80
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
# Generated documentation
55
docs/
66

7+
# Generated code coverage report
8+
coverage/
9+
710
# Local settings
811
*.local.php

Splunk/AmbiguousKeyException.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Copyright 2012 Splunk, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
* not use this file except in compliance with the License. You may obtain
7+
* a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
/**
19+
* @package Splunk
20+
*/
21+
class Splunk_AmbiguousKeyException extends RuntimeException {}

Splunk/AtomFeed.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* Copyright 2012 Splunk, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
* not use this file except in compliance with the License. You may obtain
7+
* a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
/**
19+
* Contains utilities for parsing Atom feeds received from the Splunk REST API.
20+
*
21+
* @package Splunk
22+
*/
23+
class Splunk_AtomFeed
24+
{
25+
/** Name of the 's' namespace in Splunk Atom feeds. */
26+
const NS_S = 'http://dev.splunk.com/ns/rest';
27+
28+
/**
29+
* Parses and returns the value inside the specified XML element.
30+
*
31+
* @param SimpleXMLElement $containerXml
32+
* @returns mixed
33+
*/
34+
public static function parseValueInside($containerXml)
35+
{
36+
$dictValue = $containerXml->children(Splunk_AtomFeed::NS_S)->dict;
37+
$listValue = $containerXml->children(Splunk_AtomFeed::NS_S)->list;
38+
39+
if (Splunk_XmlUtil::elementExists($dictValue))
40+
{
41+
return Splunk_AtomFeed::parseDict($dictValue);
42+
}
43+
else if (Splunk_XmlUtil::elementExists($listValue))
44+
{
45+
return Splunk_AtomFeed::parseList($listValue);
46+
}
47+
else // value is scalar
48+
{
49+
return Splunk_XmlUtil::getTextContent($containerXml);
50+
}
51+
}
52+
53+
private static function parseDict($dictXml)
54+
{
55+
$dict = array();
56+
foreach ($dictXml->children(Splunk_AtomFeed::NS_S)->key as $keyXml)
57+
{
58+
$key = Splunk_XmlUtil::getAttributeValue($keyXml, 'name');
59+
$value = Splunk_AtomFeed::parseValueInside($keyXml);
60+
61+
$dict[$key] = $value;
62+
}
63+
return $dict;
64+
}
65+
66+
private static function parseList($listXml)
67+
{
68+
$list = array();
69+
foreach ($listXml->children(Splunk_AtomFeed::NS_S)->item as $itemXml)
70+
$list[] = Splunk_AtomFeed::parseValueInside($itemXml);
71+
return $list;
72+
}
73+
}

Splunk/Collection.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Copyright 2012 Splunk, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
* not use this file except in compliance with the License. You may obtain
7+
* a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
/**
19+
* Represents a collection of entities accessible through Splunk's REST API.
20+
*
21+
* @package Splunk
22+
*/
23+
class Splunk_Collection extends Splunk_Endpoint
24+
{
25+
private $entries = NULL;
26+
27+
// === Load ===
28+
29+
protected function load()
30+
{
31+
$response = $this->service->get($this->path);
32+
$xml = new SimpleXMLElement($response->body);
33+
34+
$entries = array();
35+
foreach ($xml->entry as $entryData)
36+
{
37+
$entries[] = $this->loadEntry($entryData);
38+
}
39+
40+
$this->entries = $entries;
41+
$this->loaded = TRUE;
42+
}
43+
44+
private function loadEntry($entryData)
45+
{
46+
return new Splunk_Entity(
47+
$this->service,
48+
"{$this->path}/" . urlencode($entryData->title),
49+
$entryData);
50+
}
51+
52+
// === Children ===
53+
54+
/**
55+
* Returns the unique entity with the specified name in this collection.
56+
*
57+
* @param string $name
58+
* @return Splunk_Entity
59+
* @throws Splunk_NoSuchKeyException
60+
* @throws Splunk_AmbiguousKeyException
61+
*/
62+
public function get($name)
63+
{
64+
$results = array();
65+
foreach ($this->validate()->entries as $entry)
66+
{
67+
if ($entry->getName() == $name)
68+
{
69+
$results[] = $entry;
70+
}
71+
}
72+
73+
if (count($results) == 0)
74+
{
75+
throw new Splunk_NoSuchKeyException(
76+
"No value exists with key '{$name}'.");
77+
}
78+
else if (count($results) == 1)
79+
{
80+
return $results[0];
81+
}
82+
else
83+
{
84+
throw new Splunk_AmbiguousKeyException(
85+
"Multiple values exist with key '{$name}'. " .
86+
"Specify a namespace to disambiguate.");
87+
}
88+
}
89+
}

Splunk/Context.php

+42-7
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@
1616
*/
1717

1818
/**
19-
* This class allows clients to issue HTTP requests to a Splunk server.
19+
* Allows clients to issue HTTP requests to a Splunk server.
2020
*
2121
* @package Splunk
2222
*/
2323
class Splunk_Context
2424
{
2525
private $username;
2626
private $password;
27+
private $token;
2728
private $host;
2829
private $port;
2930
private $scheme;
3031
private $http;
3132

32-
private $token;
33-
3433
/**
3534
* @param array $args {
3635
* 'username' => (optional) The username to login with. Defaults to "admin".
3736
* 'password' => (optional) The password to login with. Defaults to "changeme".
37+
* 'token' => (optional) The authentication token to use. If provided,
38+
* the username and password are ignored and there is no
39+
* need to call login(). In the format "Splunk SESSION_KEY".
3840
* 'host' => (optional) The hostname of the Splunk server. Defaults to "localhost".
3941
* 'port' => (optional) The port of the Splunk server. Defaults to 8089.
4042
* 'scheme' => (optional) The scheme to use: either "http" or "https". Defaults to "https".
@@ -46,6 +48,7 @@ public function __construct($args)
4648
$args = array_merge(array(
4749
'username' => 'admin',
4850
'password' => 'changeme',
51+
'token' => NULL,
4952
'host' => 'localhost',
5053
'port' => 8089,
5154
'scheme' => 'https',
@@ -54,6 +57,7 @@ public function __construct($args)
5457

5558
$this->username = $args['username'];
5659
$this->password = $args['password'];
60+
$this->token = $args['token'];
5761
$this->host = $args['host'];
5862
$this->port = $args['port'];
5963
$this->scheme = $args['scheme'];
@@ -72,11 +76,25 @@ public function login()
7276
'password' => $this->password,
7377
));
7478

75-
$sessionKey = Splunk_Util::getTextContentAtXpath(
76-
new SimpleXMLElement($response['body']),
79+
$sessionKey = Splunk_XmlUtil::getTextContentAtXpath(
80+
new SimpleXMLElement($response->body),
7781
'/response/sessionKey');
7882

79-
$this->token = 'Splunk ' . $sessionKey;
83+
$this->token = "Splunk {$sessionKey}";
84+
}
85+
86+
/**
87+
* Performs an HTTP GET request to the endpoint at the specified path.
88+
*
89+
* @param string $path relative or absolute URL path.
90+
* @return array
91+
* @see Splunk_Http::get
92+
*/
93+
public function get($path)
94+
{
95+
return $this->http->get($this->url($path), array(
96+
'Authorization' => $this->token,
97+
));
8098
}
8199

82100
// === Accessors ===
@@ -92,8 +110,25 @@ public function getToken()
92110

93111
// === Utility ===
94112

113+
/**
114+
* @param string $path relative or absolute URL path.
115+
* @return string absolute URL.
116+
*/
95117
private function url($path)
96118
{
97-
return "{$this->scheme}://{$this->host}:{$this->port}{$path}";
119+
return "{$this->scheme}://{$this->host}:{$this->port}{$this->abspath($path)}";
120+
}
121+
122+
/**
123+
* @param string $path relative or absolute URL path.
124+
* @return string absolute URL path.
125+
*/
126+
private function abspath($path)
127+
{
128+
if ((strlen($path) >= 1) && ($path[0] == '/'))
129+
return $path;
130+
131+
// TODO: Support namespaces
132+
return "/services/{$path}";
98133
}
99134
}

Splunk/Endpoint.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright 2012 Splunk, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"): you may
6+
* not use this file except in compliance with the License. You may obtain
7+
* a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations
15+
* under the License.
16+
*/
17+
18+
/**
19+
* Represents a single endpoint in the Splunk REST API.
20+
*
21+
* @package Splunk
22+
*/
23+
abstract class Splunk_Endpoint
24+
{
25+
protected $service;
26+
protected $path;
27+
28+
protected $loaded = FALSE;
29+
30+
public function __construct($service, $path)
31+
{
32+
$this->service = $service;
33+
$this->path = $path;
34+
}
35+
36+
// === Load ===
37+
38+
/** Loads this resource if not already done. Returns self. */
39+
protected function validate()
40+
{
41+
if (!$this->loaded)
42+
{
43+
$this->load();
44+
assert($this->loaded);
45+
}
46+
return $this;
47+
}
48+
49+
/**
50+
* Loads this resource.
51+
*
52+
* Implementations must set $this->loaded to TRUE before returning.
53+
*/
54+
protected abstract function load();
55+
}

0 commit comments

Comments
 (0)